1

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)
Eduardo
  • 1,698
  • 4
  • 29
  • 48

1 Answers1

1
  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
    
luciomrx
  • 1,165
  • 1
  • 7
  • 7