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()