How to get value from selected item in treeview in PyGTK?
Asked Answered
D

1

16

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?

Disaccord answered 29/10, 2011 at 9:22 Comment(1)
PS: Don't forget to accept (by clicking on the "right" to the left of the answer) my answer if it suits you. Thanks !Equitant
P
27

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)
Pansophy answered 29/10, 2011 at 11:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.