简体   繁体   English

在PyGtk中设置GtkTreeViews时遇到麻烦

[英]trouble setting up GtkTreeViews in PyGtk

I've got some code in a class that extends gtk.TreeView , and this is the init method. 我在扩展gtk.TreeView的类中有一些代码,这是init方法。 I want to create a tree view that has 3 columns. 我想创建一个具有3列的树视图。 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. 下面的代码有效,不同之处在于切换按钮不会对鼠标单击做出反应,并且不会绘制标签和ComboEntry。 (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])

First of all, you need to create a model with bool , str and str columns, not the way you are doing now. 首先,您需要使用boolstrstr列创建一个模型,而不是现在的方式。 Second, you need to bind properties of renderers from appropriate model columns, eg as in 其次,您需要从适当的模型列绑定渲染器的属性,例如

self.columnButton = \
    gtk.TreeViewColumn ('Enabled', self.buttonRenderer, 
                        active = 0)  # 0 is the tree store column index

Then you need to set editable property on the renderer to True . 然后,您需要将渲染器上的editable属性设置为True And finally, you need to handle signals ( changed or editing-done , depending on renderer type) yourself and update the store accordingly. 最后,您需要自己处理信号( changedediting-done ,具体取决于渲染器类型),并相应地更新存储。

It may be easier to use some helpers, eg Py-gtktree — there's even an example for editing a tree there. 使用某些帮助程序可能会更容易,例如Py-gtktree-甚至还有一个在那里编辑树的示例。

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. 只需在gtk.CellRendererToggle中连接toggled信号,当您单击它时,它将发出该信号,然后在回调中更改模型中的值。

ej. ej。

def toggle(self, cellrenderer, path):
        Self.model[path][column] = not self.model[path][column]

self.model is the model asociated to the treeview, self.model是与树视图self.model的模型,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM