Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 398
shiii Avatar asked Oct 29 '11 09:10

shiii


1 Answers

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)
like image 190
Loïc G. Avatar answered Oct 07 '22 12:10

Loïc G.