简体   繁体   English

Python-具有CheckBox的Gtk.TreeView

[英]Python - Gtk.TreeView with CheckBox

I need to store items in a Gtk TreeView and when interacting with this TreeView, the user will can select one or more items in the list. 我需要将项目存储在Gtk TreeView中,并且与此TreeView进行交互时,用户可以在列表中选择一个或多个项目。

Because I'm new to GTK, I managed to populate the treeview and display a checkbox as the code below shows. 因为我是GTK的新手,所以设法填充了树形视图并显示了一个复选框,如下面的代码所示。 But when I try to select, nothing happens and I do not know how to make this possible. 但是,当我尝试选择时,什么也没有发生,而且我不知道如何实现这一点。

This is my Code: 这是我的代码:

# 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. If you want to select the whole row and something happen: 如果您要选择整行并发生某些情况:

     #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. If you want when you click the toggle and something happen: 如果您想要在单击切换开关时发生某些事情,请执行以下操作:

     #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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM