在 PyGtk 中设置 GtkTreeViews 时遇到问题

发布于 2024-08-31 18:36:18 字数 1259 浏览 11 评论 0原文

我在一个扩展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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二货你真萌 2024-09-07 18:36:18

首先,您需要创建一个包含 boolstrstr 列的模型,而不是您现在所做的方式。其次,您需要从适当的模型列绑定渲染器的属性,例如,如

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

然后您需要将渲染器上的 editable 属性设置为 True。最后,您需要自己处理信号(changedediting-done,具体取决于渲染器类型)并相应地更新存储。

使用一些助手可能会更容易,例如
Py-gtktree — 甚至还有一个编辑树的示例。

First of all, you need to create a model with bool, str and str columns, not the way you are doing now. Second, you need to bind properties of renderers from appropriate model columns, e.g. 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. And finally, you need to handle signals (changed or editing-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.

追风人 2024-09-07 18:36:18

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

ej。

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

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.

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

self.model is the model asociated to the treeview,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文