Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 1668  / 1 Year 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.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.


More From » python

 Answers
0

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.


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

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

Location: Bonaire
Member since Sat, Sep 24, 2022
2 Years ago
sweetrifiabl questions
Mon, Apr 4, 22, 16:02, 2 Years ago
Sat, Jul 17, 21, 01:40, 3 Years ago
Thu, Nov 18, 21, 17:36, 2 Years ago
Mon, Jan 17, 22, 19:02, 2 Years ago
;