3

I'm writing an app using Gtk/PyGI, and I need to create a sidebar with a Nautilus-like tree.

However, Gtk refuses to cooperate with me. I tried both with Glade and GtkBuilder (creating the GtkTreeStore with one gchararray column) and with this code:

def create_widgets(self):
  self.hbox = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL)
  self.add(self.hbox)
  self.treestore = Gtk.TreeStore(str)
  self.treeview = Gtk.TreeView(self.treestore)
  self.treestore.append(["Item 1"])
  self.treestore.append(["Item 2"])
  self.hbox.pack_start(self.treeview, True, True, 0)

Both versions raise a TypeError: Expected Gtk.TreeIter, but got StructMeta starting at the first self.treestore.append() in my code and trickling down through the library's files.

I thought it's because GtkTreeStore's .append also takes a GtkTreeIter, but if I pass something like "0" as the first argument nothing changes, if I pass something like self.treestore.get_iter("0") it errors out saying it's an invalid tree path, and if I pass None it works, but shows an empty window.

I tried replacing the GtkTreeStore with a GtkListStore. The result doesn't error out - instead, it shows the very same empty window as before.

So, what am I doing wrong? I went through the docs time and time again, but I still can't figure it out.

2 Answers 2

4

A Gtk.TreeStore and Gtk.ListStore are different in that a Gtk.ListStore is a flat list where you append rows as a list, whereas a Gtk.TreeStore holds parent/child data, so it takes the parent as Gtk.TreeIter and a list as row.

parent_iter = treestore.append(None, ["parent row"])
treestore.append(parent_iter, ["child row"])

http://python-gtk-3-tutorial.readthedocs.org/en/latest/treeview.html#Gtk.TreeStore.append

As for the empty window, have a look at the views section of the docs.

1
  • Thanks, the views section was what I needed. Could have sworn I tried adding a column before, but it didn't work. Oh well.
    – Andy C.
    Jun 25, 2012 at 11:14
0

I came across this demo which uses a TreeStore: http://git.gnome.org/browse/pygobject/tree/demos/gtk-demo/gtk-demo.py

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .