问题
Apparently the "proper" way to have items in a GTKListBox or Treeview is constructing with a listStore model, in the constructor. What if I want to use Glade GUI project, in which a list box is already created, and referenced by builder.get_object("appsDocumentListBox")
?
Can I set the model after Gtk.builder created the window, or is there a better way to do this?
I'm also wondering what the performance improvement is of using the ListStore vs manually adding with row = Gtk.ListBoxRow()
, adding contents and setting ListBox.add(row)
? (which does work from a Glade-builder Python window)
Unlike Treeview, apparently Listbox won't set a model after constructor?
>>> l = Gtk.ListBox()
>>> l.set_model
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'ListBox' object has no attribute 'set_model'
回答1:
You are mixing two things up. You can create a GtkListBox
and add GtkListBoxRow
s to it. There is no need for an extra model here.
There are also GtkListStore
and GtkTreeStore
. Both of them use a GtkTreeView
. The ListStore has a flat hierarchy and the TreeStore can be nested.
The GtkTreeView has a set_model
function so you can set a model after you created it. You can also create the corresponding model directly in Glade.
If you have complicated widgets that you want to add, the GtkListBox
is better suited. For a lot of data which is supposed to be sorted or hierarchically structured, I would rather use a GtkTreeView
with a corresponding model.
来源:https://stackoverflow.com/questions/33470680/how-do-i-set-a-liststore-model-on-pre-created-listbox-from-glade