PyGObject和Gtk.TreeStore / TreeView - 如何访问父元素?

时间:2018-06-08 10:13:16

标签: gtk3 pygobject

我正在使用PyGObject,我成功设置了TreeStore和相应的TreeView。这只是一个简单的单列视图。它将所有帐户列为父项,然后您可以单击小三角形并显示文件夹。代码如下所示:

    accounts_tree_store = Gtk.TreeStore(str)
    treeview_accounts = self.builder.get_object("treeview_accounts")
    treeview_accounts.set_model(accounts_tree_store)
    renderer = Gtk.CellRendererText()
    account_iter = accounts_tree_store.append(None, ["Account1"])
    accounts_tree_store.append(account_iter, ["Folder1"])
    accounts_tree_store.append(account_iter, ["Folder2"])
    accounts_tree_store.append(account_iter, ["Folder3"])
    accounts_tree_store.append(account_iter, ["Folder4"])
    accounts_tree_store.append(account_iter, ["Folder5"])

然后我添加了这个,所以我可以选择:

    selected_tree = treeview_accounts.get_selection()
    selected_tree.connect("changed", Handler().on_tree_select_change) 

我的函数处理程序如下所示:

    def on_tree_select_change(self, widget, *args):
    model, iter = widget.get_selected()            
    if iter:
    print((model[iter][0]))

现在这一切都很好。但我想打印出所选元素的父级。类似于:“Account4 for Folder4”。问题是:我如何访问父母?有某种“get_parent()”函数吗?我没有在文档中找到任何内容。有谁知道如何做到这一点?

提前致谢!!

1 个答案:

答案 0 :(得分:1)

此功能称为iter_parent,如果iter有一个,则会返回父级。这是model的方法。

model, iter = widget.get_selected()            
parent = model.iter_parent (iter)