gpt4 book ai didi

python - 让复选框在 GtkTreeView 中切换

转载 作者:太空宇宙 更新时间:2023-11-04 01:40:20 24 4
gpt4 key购买 nike

我正在使用带有复选框的 TreeView 。我希望用户能够单击一个复选框,它将将该项目添加到收藏夹列表中。但目前我根本无法让盒子切换。这是我的代码:

def draw_columns(self,treeview):
self.ren = gtk.CellRendererToggle()
self.ren.connect('toggled',self.on_toggle,treeview.get_model())
self.tvfav = gtk.TreeViewColumn('Fav',self.ren,text=7)
for i in [self.tvfav,'andall the other columns']:
treeview.append_column(i)

def on_toggle(self,cell,path_str,model):
toggle_item = model.get_value(iter,column)
toggle_item = not toggle_item
# This method didn't work either
## model[path_str][1] = not model[path_str][1]
if toggle_item:
#Add it to the favourite list if it isn't already
pass
else:
#remove it from the favourite list
pass
model.set(iter,column,toggle_item)

def __init__(self):'
....
self.liststore = gtk.ListStore(str,int, int, int,str, 'gboolean', str)
self.treeview = gtk.TreeView(self.liststore)
....

无法选中这些框我做错了什么?另外,当项目像这样附加到 TreeView 时,我将如何设置切换:

if name in favourites:
#Append to list with checkbox on
self.liststore.append([name,x,y,z,ss,True,sss])

最佳答案

免责声明:我相信这不是废话,但目前无法测试。

首先,CellRendererToggle 不会采用文本属性。其次,如果无论如何都要设置它,则不会将其设置为列索引 7,因为列表存储中只有 7 列(索引 7 将是第 8 列)。

您可以在 reference 中查看可以为渲染器设置的所有可用属性在“属性”下(另请注意继承的属性)。现在,要为每个单独的单元格(每行)设置属性,您可以像以前一样指定一个关键字参数。所以在你的 TreeviewColumn 中你会这样设置:

# 5 is the index of bool in the liststore
gtk.TreeViewColumn('Fav',renderer,active=5)

像这样的任何属性集都映射到列表存储中的相应条目。这意味着您可以直接在列表存储上或通过回调(例如 on_toggle)更改它。

编辑:

也许你必须设置 mode property还有

编辑 2:

这是一个工作示例。

import gtk


def on_toggle(cell, path, model, *ignore):
if path is not None:
it = model.get_iter(path)
model[it][0] = not model[it][0]

model = gtk.ListStore(bool)
tv = gtk.TreeView(model)

cell = gtk.CellRendererToggle()
cell.connect("toggled", on_toggle, model)
col = gtk.TreeViewColumn("Foo", cell, active=0)
tv.append_column(col)

w = gtk.Window()
w.connect("destroy", gtk.main_quit)
w.show()

w.add(tv)
tv.show()

## Some initial data
model.append([True])
model.append([False])

gtk.main()

关于python - 让复选框在 GtkTreeView 中切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5707495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com