在Gtk + 3上,我使用TreeModel
來存儲嵌套信息,並使用CellRendererText
和CellRendererToggle
來顯示它。用戶可以在每個切換按鈕上點擊,當有嵌套的時候,他們不在同一狀態,我想讓上層的人反映「不一致」狀態。我如何設置這個屬性爲單元?將CellRendererToggle的單行設置爲不一致
爲了更清楚,什麼我想從傳輸BitTorrent客戶端實現的例子:
我知道如何在單元格渲染器的所有按鈕設置爲不一致狀態myCellRendererToggle.set_properties(inconsistent=True)
但似乎我不能從這裏訪問單個元素;
我知道如何訪問我的TreeStore模型的特定行,但我只能設置«True»和«False»值。
我的代碼是相當接近的官方文檔,以便你能幫助我與它:https://python-gtk-3-tutorial.readthedocs.org/en/latest/cellrenderers.html#cellrenderertoggle(我用的,而不是一個listStore一個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.
我必須再試一次。 我想知道...我們是否真的必須手動執行它,難道它不是樹視圖中的一個功能嗎?
我已經添加了一些示例代碼(我是一個Python dilettante,但我希望它顯示了這一點)。如果第一個文本列的值爲「不一致」,則切換列也會設置爲不一致。如果第一列的文本值爲「一致」,則切換列設置爲「正常」狀態(根本不切換)。 –