How to get value from selected item in treeview in PyGTK?

16,034

You may use the gtk.TreeView.get_selection() method to get the gtk.TreeSelection.

Next, you should use the gtk.TreeSelection.get_selected_rows() method to get the TreeModel (the ListStore) and the selected items paths.

Then, you can use the gtk.TreeModel.get_iter() in order to get the iter from the path (returned by the gtk.TreeSelection.get_selected_rows() method).

Finally, you may use the gtk.TreeModel.get_value() method to get the value corresponding to the column and the iter previously recovered.

Example :

def onSelectionChanged(tree_selection) :
    (model, pathlist) = tree_selection.get_selected_rows()
    for path in pathlist :
        tree_iter = model.get_iter(path)
        value = model.get_value(tree_iter,0)
        print value

listStore = gtk.ListStore(int)
treeview = gtk.TreeView()
treeview.set_model(listStore)
tree_selection = treeview.get_selection()
tree_selection.set_mode(gtk.SELECTION_MULTIPLE)
tree_selection.connect("changed", onSelectionChanged)
Share:
16,034
shiii
Author by

shiii

Updated on June 05, 2022

Comments

  • shiii
    shiii almost 2 years

    I'm learning PyGtk. I have a simple treeview with 1 column, I get items for that treeview from list.

    How to get value of selected item in treeview?

    • Loïc G.
      Loïc G. over 12 years
      PS: Don't forget to accept (by clicking on the "right" to the left of the answer) my answer if it suits you. Thanks !