在 GtkTreeView 中切换复选框

发布于 2024-11-01 23:44:13 字数 1147 浏览 8 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不气馁 2024-11-08 23:44:13

免责声明:我相信这不是完全胡说八道,但目前无法测试它。

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

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

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

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

编辑:

也许您必须设置 模式属性< /a> 以及

编辑 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()

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:

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

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.

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文