在 Gtk+3 上,我使用 aTreeModel
来存储嵌套信息,并用 aCellRendererText
和 a显示它CellRendererToggle
。用户可以点击每个切换按钮,当有嵌套的按钮并且它们处于不同的状态时,我希望上层的按钮反映“不一致”状态。如何为一个元素设置此属性?
为了更清楚起见,我想从传输 bittorrent 客户端实现的示例:
我知道如何将单元格渲染器的所有按钮设置为不一致的状态,myCellRendererToggle.set_properties(inconsistent=True)
但似乎我无法从这里访问一个元素;
我知道如何访问我的 TreeStore 模型的特定行,但我只能设置 «True» 和 «False» 值。
我的代码非常接近官方文档,因此您可以帮助我:https ://python-gtk-3-tutorial.readthedocs.org/en/latest/cellrenderers.html#cellrenderertoggle(我使用 treeStore 而不是列表存储)
这是我的代码:
class HelloMyApp:
def __init__(self):
# Set the Glade file
self.builder = Gtk.Builder()
self.builder.add_from_file(GLADEFILE)
dic = {
"on_button1_clicked" : self.btnValidate_clicked,
"on_MainWindow_destroy" : self.quit,
"on_window1_delete_event" : self.quit,
}
self.builder.connect_signals(dic)
window = self.builder.get_object("window1")
treeview1 = self.builder.get_object("treeview1")
######## This is my model : it stores a string and a boolean. #########
self.treeModel = Gtk.TreeStore(str, bool)
# Example on how to insert data in the model
treeIter = self.treeModel.append(None, ['example one', True])
self.treeModel.append(treeIter, [' simple elt', True])
treeIter = self.treeModel.append(treeIter, ['example two', False])
self.treeModel.append(treeIter, ['under example two', True])
select = treeview1.get_selection()
select.set_mode(Gtk.SelectionMode.BROWSE)
select.connect("changed", self.on_tree_selection_changed, buf)
# Using one column of text and another column with the toggle buttons
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn("Title", renderer, text=0)
treeview1.append_column(column)
###### Here is the CellRendereToggle ################
renderer_toggle = Gtk.CellRendererToggle()
renderer_toggle.connect("toggled", self.on_cell_toggled)
column_toggle = Gtk.TreeViewColumn("Installer", renderer_toggle, active=1)
treeview1.append_column(column_toggle)
treeview1.set_model(self.treeModel)
window.show_all()
if __name__ == "__main__":
HelloMyApp = HelloMyApp()
Gtk.main()
谢谢 !
编辑:回答马库斯:我想不通,当我在函数内更改 CellRendererToggle 的属性时,它会更改每一行。
编辑解决方案:正如 Marcu 指出的那样,我们必须在每种情况下设置属性,这就是我添加该else
部分的原因。
def cellRenderer_func(column, cellRenderer, treeModel, treeIter, userData):
if 'cat' in treeModel.get_value(treeIter, 0):
# it happens only ones in my model,
# so here I am in a row I want to change to inconsistent.
cellRenderer.set_property('inconsistent',True)
# I was expecting that changes the box of that row but it affects every row.
else:
cellRenderer.set_property('inconsistent', False) # and that's ok now.
我得再试一次。我想知道......我们真的必须手动完成吗,它不能成为 treeView 的开箱即用功能吗?