Frage

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

War es hilfreich?

Lösung

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.

Andere Tipps

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]

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top