获取Gtk TreeView的行号 [英] Getting the row number of Gtk TreeView

查看:147
本文介绍了获取Gtk TreeView的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a GTk TreeView called MyTree with the following data as presented below,

DTime  ATime Transfer Platform

14:30    15:20    0       2a

14:50    15:40    0       14b

15:00    16:00    2       3a

您可以看到我有3行4列.因此,我需要获取选定的行.我是这样做的,

As you can see I have 3 rows with 4 columns. 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()

这时,它返回树iter,该树iter指向当前选定的行.一切都很好.但是我有兴趣知道iter指向第0、1还是2行.

At this point it returns the tree iter which points at the currently selected row. This is all fine. However I am interested in knowing whether iter is pointing at row 0, 1 or 2.

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

I hope I have made this clear. I need the row index and not the row iter. How do I get the row number?

推荐答案

由于您处于浏览选择模式,因此您只知道一行被选中.然后,您可以获取到第一个选定项目的路径

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]

然后,如果您的树只有一个级别(例如,它不是嵌套树),则可以从这样的路径获取索引

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]

这看起来很复杂,但这是因为您的选择可能包含许多行(这就是为什么它返回列表)并且因为树可以具有许多级别(这就是为什么它返回路径)的原因.

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).

这篇关于获取Gtk TreeView的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆