python - 如何让 GtkListStore 连续存储对象属性?

标签 python gtk pygtk

我正在尝试使用我找到的代码段将非文本对象保留在 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 propertytext' of type gchararray' from value of typedata+TrayIcon+Series'
window.show_all() Warning: unable to set property text' of typegchararray' from value of type `data+TrayIcon+Series'
gtk.main()gtk.main()

我应该怎么做才能让它发挥作用?

最佳答案

倒数第二 block 中的两个错误。

  1. GtkWarning: gtk_tree_view_column_cell_layout_set_cell_data_func: assertion `info != NULL'

    在英语中,这意味着单元格渲染器不在列的单元格渲染器列表中。在调用 set_cell_data_func 之前,您需要先将单元格渲染器添加到列中。

  2. 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/

相关文章:

python - 使用 pip (CPU 版本)在 Windows 10 中安装 TensorFlow 时出现错误

python - uwsgi - 不使用 virtualenv 中的 python2.7.3,而是使用 venv 中的 2.6,即使 2.6 仅在全局安装

python - 用于消除位信号噪声尖峰的正则表达式

python - 在 pygtk 上打开和关闭子窗口

python - 如何使用 pygtk 创建弹出窗口?

python - 将 PyGTK 脚本转换为 EXE 会导致旧的 Windows 2K GUI 外观

python - 每个示例具有不同权重的 Keras 自定义损失函数

C Gtk 问题 : Adding a VBox to the Main Window

python - Gtk警告: could not open display

python - GTK 对话框边距不起作用