Thursday, September 28, 2023
 Popular · Latest · Hot · Upcoming
rated 0 times [  4] [ 0]  / answers: 1 / hits: 1561  / 5 Months ago, sat, may 6, 2023, 9:32:32

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

More From » python


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

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

[#37302] Saturday, May 6, 2023, 5 Months  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 422
Total Questions: 94
Total Answers: 120

Location: Bonaire
Member since Sat, Sep 24, 2022
1 Year ago
sweetrifiabl questions
Mon, Apr 4, 22, 16:02, 2 Years ago
Sat, Jul 17, 21, 01:40, 2 Years ago
Thu, Nov 18, 21, 17:36, 2 Years ago
Mon, Jan 17, 22, 19:02, 2 Years ago
Fri, Dec 9, 22, 20:16, 10 Months ago