我正在尝试使用我找到的代码段将非文本对象保留在 ListStore 中。这些是对象:
class Series(gobject.GObject, object):
def __init__(self, title):
super(Series, self).__init__()
self.title = title
gobject.type_register(Series)
class SeriesListStore(gtk.ListStore):
def __init__(self):
super(SeriesListStore, self).__init__(Series)
self._col_types = [Series]
def get_n_columns(self):
return len(self._col_types)
def get_column_type(self, index):
return self._col_types[index]
def get_value(self, iter, column):
obj = gtk.ListStore.get_value(self, iter, 0)
return obj
现在我试图让 TreeView 显示它:
...
liststore = SeriesListStore()
liststore.clear()
for title in full_conf['featuring']:
series = Series(title)
liststore.append([series])
def get_series_title(column, cell, model, iter):
cell.set_property('text', liststore.get_value(iter, column).title)
return
selected = builder.get_object("trvMain")
selected.set_model(liststore)
col = gtk.TreeViewColumn(_("Series title"))
cell = gtk.CellRendererText()
col.set_cell_data_func(cell, get_series_title)
col.pack_start(cell)
col.add_attribute(cell, "text", 0)
selected.append_column(col)
...
但它失败并出现错误:
GtkWarning: gtk_tree_view_column_cell_layout_set_cell_data_func: assertion
info != NULL' failed<br/> col.set_cell_data_func(cell, get_series_title) Warning: unable to set property
text' of typegchararray' from value of type
data+TrayIcon+Series'
window.show_all() Warning: unable to set propertytext' of type
gchararray' from value of type `data+TrayIcon+Series'
gtk.main()gtk.main()
我应该怎么做才能让它发挥作用?
最佳答案
倒数第二 block 中的两个错误。
-
GtkWarning: gtk_tree_view_column_cell_layout_set_cell_data_func: assertion `info != NULL'
在英语中,这意味着单元格渲染器不在列的单元格渲染器列表中。在调用
set_cell_data_func
之前,您需要先将单元格渲染器添加到列中。 -
Warning: unable to set property `text' of type `gchararray' from value of `typedata+TrayIcon+Series'
这是因为
add_attribute
行导致 GTK+ 尝试将单元格文本设置为 Series 对象,这当然会失败。只需删除该行;单元格数据函数已经负责设置单元格文本。
在代码中:
col = gtk.TreeViewColumn(_("Series title"))
cell = gtk.CellRendererText()
col.pack_start(cell)
col.set_cell_data_func(cell, get_series_title)
关于python - 如何让 GtkListStore 连续存储对象属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3488285/