PyGObject 和 Gtk.TreeStore / TreeView - 如何访问 parent 元素?
PyGObject and Gtk.TreeStore / TreeView - How do I acces the parent element?
我正在使用 PyGObject 并成功设置了 TreeStore 和相应的 TreeView。这只是一个简单的 one-column 视图。它将所有帐户列为 parents,然后您可以单击小三角形并显示文件夹。代码如下所示:
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]))
现在一切正常。但我还想打印出所选元素的 parent 。类似于:"Folder2 for Account4"。问题是:如何访问 parent?有某种 "get_parent()" 功能吗?我没有在文档中找到任何内容。有谁知道如何做到这一点?
提前致谢!!
此函数称为 iter_parent,return% 的 iter
有一个。这是model
的方法。
model, iter = widget.get_selected()
parent = model.iter_parent (iter)
我正在使用 PyGObject 并成功设置了 TreeStore 和相应的 TreeView。这只是一个简单的 one-column 视图。它将所有帐户列为 parents,然后您可以单击小三角形并显示文件夹。代码如下所示:
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]))
现在一切正常。但我还想打印出所选元素的 parent 。类似于:"Folder2 for Account4"。问题是:如何访问 parent?有某种 "get_parent()" 功能吗?我没有在文档中找到任何内容。有谁知道如何做到这一点?
提前致谢!!
此函数称为 iter_parent,return% 的 iter
有一个。这是model
的方法。
model, iter = widget.get_selected()
parent = model.iter_parent (iter)