简体   繁体   English

获取Gtk TreeView的行号

[英]Getting the row number of Gtk TreeView

I have a GTk TreeView called MyTree with the following data as presented below, 我有一个名为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

As you can see I have 3 rows with 4 columns. 如你所见,我有3行,有4列。 So I need to get the selected row. 所以我需要获取所选行。 I did this by, 我是这样做的,

selection = MyTree.get_selection()
selection.set_mode(Gtk.SelectionMode.BROWSE)
model, iter = selection.get_selected()

At this point it returns the tree iter which points at the currently selected row. 此时它返回指向当前所选行的树iter。 This is all fine. 这一切都很好。 However I am interested in knowing whether iter is pointing at row 0, 1 or 2. 但是我有兴趣知道它是否指向第0,1或2行。

I hope I have made this clear. 我希望我已经说清楚了。 I need the row index and not the row iter. 我需要行索引而不是行iter。 How do I get the row number? 我如何获得行号?

Since you are in BROWSE selection mode, you know there is only one selected row. 由于您处于BROWSE选择模式,因此您知道只有一个选定的行。 Then, you can get the path to the first selected item doing 然后,您可以获得第一个选定项目的路径

path = iter.get_selected_rows()[0]

Then, if your tree has only one level (eg it is not a nested tree), you can get the index from the path like this 然后,如果您的树只有一个级别(例如,它不是嵌套树),您可以从这样的路径获取索引

index = path.get_indices()[0]

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). 这似乎很复杂,但这是因为你的选择可能包含很多行(这就是为什么它返回一个列表),因为树可以有很多级别(这就是它返回路径的原因)。

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

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