在Gtk 3上,我使用TreeModel存储嵌套信息,并使用CellRendererText和CellRendererToggle显示它.用户可以在每个切换按钮上进行clic,当有嵌套按钮并且它们不处于相同状态时,我希望上层的按钮反映“不一致”状态.如何为一个元素设置此属性?
为了更清楚,我想从Transmission bittorrent客户端实现的一个例子:
我知道如何使用myCellRendererToggle.set_properties(不一致= True)将单元格渲染器的所有按钮设置为不一致状态,但似乎我无法从此处访问单个元素;
我知道如何访问我的TreeStore模型的特定行,但我只能设置«True»和«False»值.
这是我的代码:
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.
我得再试一次.
我想知道……我们真的必须手动完成它,它不能成为一个开箱即用的功能吗?