简体   繁体   中英

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.

Because I'm new to GTK, I managed to populate the treeview and display a checkbox as the code below shows. 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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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