Python - 带CheckBox的Gtk.TreeView

时间:2014-07-25 19:16:19

标签: python list python-2.7 gtk gtktreeview

我需要将项目存储在Gtk TreeView中,当与此TreeView交互时,用户可以选择列表中的一个或多个项目。

因为我是GTK的新手,所以我设法填充了树视图并显示了一个复选框,如下面的代码所示。但是当我尝试选择时,没有任何反应,我不知道如何做到这一点。

这是我的代码:

# the column is created
renderer_products = gtk.CellRendererText()
column_products = gtk.TreeViewColumn("Products", renderer_products, text=0)
# and it is appended to the treeview
view.append_column(column_products)

# the column checkbox is created
renderer_checkbox = gtk.CellRendererToggle()
column_checkbox = gtk.TreeViewColumn("Selected", renderer_checkbox, text=0)
# and it is appended to the treeview
view.append_column(column_checkbox)

1 个答案:

答案 0 :(得分:1)

  1. 如果您想选择整行并发生一些事情:

    #double click or not double click use
    Gtk.TreeView.set_activate_on_single_click (bool)
    #connect the treeview
    treeview.connect ("row-activated", on_row_activate)
    #inside the callback
    def on_row_activate (treeview, path, column):
        model = treeview.get_model ()
        iter  = treeview.get_iter (path)
        yourdata = model[iter][model_index]
        #do whatever with yourdata
    
  2. 如果您想在单击切换时想要发生某事:

    #connect the renderer
    renderer_checkbox.connect ("toggled", on_selected_toggled)
    #inside the callback
    def on_selected_toggled (renderer, path):
        #modify the model or get the value or whatever