在 PyGtk 中设置 GtkTreeViews 时遇到问题
我在一个扩展gtk.TreeView
的类中得到了一些代码,这就是init
方法。我想创建一个包含 3 列的树视图。一个切换按钮、一个标签和一个用户可以在其中输入内容的下拉框。下面的代码可以工作,只是切换按钮不会对鼠标单击做出反应,并且不会绘制标签和 ComboEntry。 (所以我想你可以说它不起作用)。不过,我可以很好地添加行。
#make storage enable/disable label user entry
self.tv_store = gtk.TreeStore(gtk.ToggleButton, str, gtk.ComboBoxEntry)
#make widget
gtk.TreeView.__init__(self, self.tv_store)
#make renderers
self.buttonRenderer = gtk.CellRendererToggle()
self.labelRenderer = gtk.CellRendererText()
self.entryRenderer = gtk.CellRendererCombo()
#make columns
self.columnButton = gtk.TreeViewColumn('Enabled')
self.columnButton.pack_start(self.buttonRenderer, False)
self.columnLabel = gtk.TreeViewColumn('Label')
self.columnLabel.pack_start(self.labelRenderer, False)
self.columnEntry = gtk.TreeViewColumn('Data')
self.columnEntry.pack_start(self.entryRenderer, True)
self.append_column(self.columnButton)
self.append_column(self.columnLabel)
self.append_column(self.columnEntry)
self.tmpButton = gtk.ToggleButton('example')
self.tmpCombo = gtk.ComboBoxEntry(None)
self.tv_store.insert(None, 0, [self.tmpButton, 'example label', self.tmpCombo])
I've got some code in a class that extends gtk.TreeView
, and this is the init
method. I want to create a tree view that has 3 columns. A toggle button, a label, and a drop down box that the user can type stuff into. The code below works, except that the toggle button doesn't react to mouse clicks and the label and the ComboEntry aren't drawn. (So I guess you can say it doesn't work). I can add rows just fine however.
#make storage enable/disable label user entry
self.tv_store = gtk.TreeStore(gtk.ToggleButton, str, gtk.ComboBoxEntry)
#make widget
gtk.TreeView.__init__(self, self.tv_store)
#make renderers
self.buttonRenderer = gtk.CellRendererToggle()
self.labelRenderer = gtk.CellRendererText()
self.entryRenderer = gtk.CellRendererCombo()
#make columns
self.columnButton = gtk.TreeViewColumn('Enabled')
self.columnButton.pack_start(self.buttonRenderer, False)
self.columnLabel = gtk.TreeViewColumn('Label')
self.columnLabel.pack_start(self.labelRenderer, False)
self.columnEntry = gtk.TreeViewColumn('Data')
self.columnEntry.pack_start(self.entryRenderer, True)
self.append_column(self.columnButton)
self.append_column(self.columnLabel)
self.append_column(self.columnEntry)
self.tmpButton = gtk.ToggleButton('example')
self.tmpCombo = gtk.ComboBoxEntry(None)
self.tv_store.insert(None, 0, [self.tmpButton, 'example label', self.tmpCombo])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您需要创建一个包含
bool
、str
和str
列的模型,而不是您现在所做的方式。其次,您需要从适当的模型列绑定渲染器的属性,例如,如然后您需要将渲染器上的
editable
属性设置为True
。最后,您需要自己处理信号(changed
或editing-done
,具体取决于渲染器类型)并相应地更新存储。使用一些助手可能会更容易,例如
Py-gtktree — 甚至还有一个编辑树的示例。
First of all, you need to create a model with
bool
,str
andstr
columns, not the way you are doing now. Second, you need to bind properties of renderers from appropriate model columns, e.g. as inThen you need to set
editable
property on the renderer toTrue
. And finally, you need to handle signals (changed
orediting-done
, depending on renderer type) yourself and update the store accordingly.It may be easier to use some helpers, e.g.
Py-gtktree — there's even an example for editing a tree there.
只需连接 gtk.CellRendererToggle 中的
toggled
信号,当您单击它时,它将发出该信号,然后在回调中更改模型中的值。ej。
self.model
是与树视图关联的模型,Just connnect the
toggled
signal in the gtk.CellRendererToggle, when you click on it, it will emit that signal, then in your callback change the value in the model.ej.
self.model
is the model asociated to the treeview,