在 GtkTreeView 中切换复选框
我正在使用带有复选框的树视图。我希望用户能够单击一个复选框,然后将该项目添加到收藏夹列表中。但目前我根本无法切换这些框。这是我的代码:
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)
....
我做错了什么,无法检查这些框?另外,当项目附加到树视图时,我将如何设置切换,如下所示:
if name in favourites:
#Append to list with checkbox on
self.liststore.append([name,x,y,z,ss,True,sss])
I am using a treeview with checkboxes. I want the user to be able to click a checkbox and it will add that item to a favourites list. But currently I cannot get the boxes to toggle at all. Here's my code:
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)
....
What am I doing wrong that the boxes can't be checked? Also, how would I set the toggle when items are appended to the treeview like this:
if name in favourites:
#Append to list with checkbox on
self.liststore.append([name,x,y,z,ss,True,sss])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
免责声明:我相信这不是完全胡说八道,但目前无法测试它。
首先,CellRendererToggle 不会采用文本属性。其次,如果您无论如何都要设置它,则不会将其设置为列索引 7,因为列表存储上只有 7 列(索引 7 将是第八列)。
您可以在参考中查看可以为渲染器设置的所有可用属性在“属性”下(另请注意继承的属性)。现在,要为每个单元格(每行)设置属性,您可以像以前一样指定关键字参数。因此,在 TreeviewColumn 中,您可以这样设置:
像这样设置的任何属性都会映射到列表存储中的相应条目。这意味着您可以直接在列表存储上或通过回调(例如 on_toggle)更改它。
编辑:
也许您必须设置 模式属性< /a> 以及
编辑 2:
这是一个工作示例。
Disclaimer: I am confident this is not total bs, but can't test it at the moment.
First, a CellRendererToggle wouldn't take a text property. second, if you would set it anyway, you wouldn't set it to column index 7 because you only have 7 column on the liststore (and index 7 would be the eigth column).
You can see all available properties you can set for your renderer in the reference under 'Properties' (note also the inherited properties). Now, to set the property for each individual cell (per row) you can specify as you did, a keyword argument. So in your TreeviewColumn you would set this:
Any attribute set like this is mapped to corresponding entry in the liststore. That means you can directy change it on the liststore or via a callback (on_toggle for instance).
Edit:
Maybe you have to set the mode property as well
Edit 2:
Here is a working example.