Gtk.Treeview get current row index in Python
Asked Answered
A

3

9

How to get index of current selected row in Gtk.TreeView in Python ?

Anitraaniweta answered 10/7, 2012 at 5:59 Comment(0)
L
8

You can call gtk.TreeView.get_selection to get the current selection (gtk.TreeSelection). You can then call gtk.TreeSelection.get_selected to get:

a 2-tuple containing a reference to the gtk.TreeModel and a gtk.TreeIter pointing to the currently selected node.

The iter can be used on a gtk.TreeModel (which is obtained by calling gtk.TreeView.get_model. You can then use gtk.TreeModel.get_value to get any of the column values of the node at that position in the tree.

Laminar answered 10/7, 2012 at 8:25 Comment(0)
G
14

The other answer is likely more useful in general. However, to answer the actual question posed by the OP, how to get the row index: assuming one row is selected, you can get it with:

index = treeview.get_selection().get_selected_rows()[1][0][0]
Guaiacum answered 23/1, 2019 at 0:35 Comment(1)
Thank! i spent hours looking for this! How one is suposed to know that ? its almost a haack :)Roselani
L
8

You can call gtk.TreeView.get_selection to get the current selection (gtk.TreeSelection). You can then call gtk.TreeSelection.get_selected to get:

a 2-tuple containing a reference to the gtk.TreeModel and a gtk.TreeIter pointing to the currently selected node.

The iter can be used on a gtk.TreeModel (which is obtained by calling gtk.TreeView.get_model. You can then use gtk.TreeModel.get_value to get any of the column values of the node at that position in the tree.

Laminar answered 10/7, 2012 at 8:25 Comment(0)
F
1

Somewhat late, perhaps not so relevant, but if you have double clicked on the selected row you will get the row index as element of the TreePath tuple, like so

    def on_row_activated(self, treeview, path, column):
        model = self.treeview.get_model()
        tree_iter = model.get_iter(path)
        row = path[0]
        if tree_iter:
            # here you can get the values of the columns
Fresco answered 17/2, 2021 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.