GtkTreeView with multiple columns and GtkListStore with single custom type (in Python)

◇◆丶佛笑我妖孽 提交于 2019-11-29 22:13:17

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/22825766/gtktreeview-with-multiple-columns-and-gtkliststore-with-single-custom-type-in-p

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!