带有多个列的GtkTreeView和带有单个自定义类型的GtkListStore(在Python中) [英] GtkTreeView with multiple columns and GtkListStore with single custom type (in Python)

查看:420
本文介绍了带有多个列的GtkTreeView和带有单个自定义类型的GtkListStore(在Python中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Gtk.TreeView 的不同列上显示对象的属性。假设我有以下内容:

$ p $ class MyClass(GObject.GObject):
def __init __(self,first,last ,年龄):
self.first = first
self.last = last
self.age = age

我想将实例存储在 Gtk.ListStore 中,如下所示。

  store = Gtk.ListStore(MyClass)

现在,当创建 Gtk.TreeView 时,我不知道如何指定必须呈现2列,其中一列用于 first property,另一个为 age 属性。

  view = Gtk.TreeView(model = store)
#在这里添加第一个和年龄的列
...

这些帖子(1)(2)在某种程度上解释了如何使用自定义类型,但只使用1列(即 store 匹配视图中的列数)。我听起来像我试图做的事情是常见的事情,而不需要任何解决方法。也许它是关于 Gtk.ListStore 的子类,让它告诉视图它有几列和如何获取每个值?



另外,我怎样才能自动通知 store 中MyClass实例的变化,反映在视图

解决方案

您需要做两件事:使用单个 TYPE_PYOBJECT 列设置 ListStore ,然后使用 set_cell_data_func

以下示例演示了这一点:

$ b $在树视图列中设置元素c $ c>来设置适当对象属性的单元格文本。 b

  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)])
$ b $ 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
$ $ b $ 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(查看)
w.show_all()
Gtk.main()


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

这篇关于带有多个列的GtkTreeView和带有单个自定义类型的GtkListStore(在Python中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆