I'm trying to display properties of an object on different columns of a Gtk.TreeView
. Say I have the following:
class MyClass(GObject.GObject):
def __init__(self, first, last, age):
self.first = first
self.last = last
self.age = age
And I want to store instances in a Gtk.ListStore
as shown below.
store = Gtk.ListStore(MyClass)
Now, when creating the Gtk.TreeView
, I don't know how to specify that 2 columns must be rendered, one for the first
property and the other for the age
property.
view = Gtk.TreeView(model=store)
# Columns for first and age added here
...
These posts (1) and (2) somewhat explain how to use custom types, but only use 1 column (then the length of the number of columns in the store
matches the number of columns in the view
). I sounds like what I'm trying to do would be a common thing without needing any workarounds. Perhaps it's about sub-classing Gtk.ListStore
to have it tell the view
that it has several columns and how to fetch each value?
Also, how do I make so that changes to MyClass instances in the store
are automatically notified and reflected on the view
?
You need to do two things: set up a ListStore
with a single TYPE_PYOBJECT
column, and then use a set_cell_data_func
on the tree view columns to set the cell text from the appropriate object attributes.
Here is an example that demonstrates this:
from gi.repository import Gtk, GObject
class MyClass(object):
def __init__(self, first, last, age):
self.first = first
self.last = last
self.age = age
tree_store = Gtk.ListStore(GObject.TYPE_PYOBJECT)
tree_store.append([MyClass("foo", "bar", 15)])
tree_store.append([MyClass("baz", "qux", 100)])
def make_column(caption, getter):
rend = Gtk.CellRendererText()
col = Gtk.TreeViewColumn(caption, rend)
def _set_cell_text(column, cell, model, it, ignored):
obj = model.get_value(it, 0)
cell.set_property('text', getter(obj))
col.set_cell_data_func(rend, _set_cell_text)
return col
view = Gtk.TreeView(tree_store)
view.append_column(make_column("First", lambda obj: obj.first))
view.append_column(make_column("Last", lambda obj: obj.last))
view.append_column(make_column("Age", lambda obj: '%d' % obj.age))
w = Gtk.Window()
w.add(view)
w.show_all()
Gtk.main()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With