casualcoder
casualcoder

Reputation: 231

PyGObject and Gtk.TreeStore / TreeView - How do I acces the parent element?

I'm working with PyGObject and I successfully setup a TreeStore and a corresponding TreeView. It is just a simple one-column view. It lists all accounts as parents and then you can click the little triangle and it shows the folders. The code looks like this:

    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"])

Then I added this so I can get a selection:

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

And my function handler looks like this:

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

Now all this works just fine. But I want to also print out the parent of the element that is selected. Something like: "Folder2 for Account4". The question is: How can I access the parent? Is there some sort of "get_parent()" function? I didn't find anything in the docs. Does anyone know how to do this?

Thanks in advance!!

Upvotes: 0

Views: 631

Answers (1)

Alexander Dmitriev
Alexander Dmitriev

Reputation: 2525

This fuction is called iter_parent and will return parent if iter has one. It's a model's method.

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

Upvotes: 1

Related Questions