获取 Gtk TreeView 的行号

Nik*_*Nik 4 python gtk

我有一个名为MyTree的 GTk TreeView ,其数据如下所示,

DTime  ATime Transfer Platform

14:30    15:20    0       2a

14:50    15:40    0       14b

15:00    16:00    2       3a
Run Code Online (Sandbox Code Playgroud)

如您所见,我有 3 行 4 列。所以我需要获取选定的行。我这样做了,

selection = MyTree.get_selection()
selection.set_mode(Gtk.SelectionMode.BROWSE)
model, iter = selection.get_selected()
Run Code Online (Sandbox Code Playgroud)

此时它返回指向当前选定行的树迭代器。这一切都很好。但是我很想知道 iter 是否指向第 0、1 或 2 行。

我希望我已经说清楚了。我需要行索引而不是行迭代器。我如何获得行号?

Cha*_*net 5

Since you are in BROWSE selection mode, you know there is only one selected row. Then, you can get the path to the first selected item doing

path = iter.get_selected_rows()[0]
Run Code Online (Sandbox Code Playgroud)

Then, if your tree has only one level (e.g. it is not a nested tree), you can get the index from the path like this

index = path.get_indices()[0]
Run Code Online (Sandbox Code Playgroud)

It seems complicated, but this is because you selection could contains many rows (this is why it returns a list) and because the tree can have many levels (this is why it returns a path).