简体   繁体   English

Gtk.Treeview 在 Python 中获取当前行索引

[英]Gtk.Treeview get current row index in Python

如何在 Python 中的 Gtk.TreeView 中获取当前选定行的索引?

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:但是,要回答 OP 提出的实际问题,如何获取行索引:假设选择了一行,您可以通过以下方式获取:

index = treeview.get_selection().get_selected_rows()[1][0][0]

You can call gtk.TreeView.get_selection to get the current selection ( gtk.TreeSelection ).您可以调用gtk.TreeView.get_selection来获取当前选择( gtk.TreeSelection )。 You can then call gtk.TreeSelection.get_selected to get:然后你可以调用gtk.TreeSelection.get_selected来获取:

a 2-tuple containing a reference to the gtk.TreeModel and a gtk.TreeIter pointing to the currently selected node.包含对 gtk.TreeModel 的引用和指向当前选定节点的 gtk.TreeIter 的 2 元组。

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. iter 可用于gtk.TreeModel (通过调用gtk.TreeView.get_model获得。然后您可以使用gtk.TreeModel.get_value获取树中该位置节点的任何列值。

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有点晚了,也许不是那么相关,但是如果您双击所选行,您将获得行索引作为 TreePath 元组的元素,就像这样

    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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM