【问题标题】:How to make GtkListStore store object attribute in a row?如何让 GtkListStore 连续存储对象属性?
【发布时间】:2010-08-15 16:47:23
【问题描述】:

我正在尝试使用我找到的 sn-p 保存 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)
    ...

但它失败并出现错误:

Gtk警告: gtk_tree_view_column_cell_layout_set_cell_data_func: 断言info != NULL' failed
col.set_cell_data_func(cell, get_series_title) Warning: unable to set property
text' 类型为gchararray' from value of typedata+TrayIcon+Series'
window.show_all() 警告:无法从值设置属性text' of typegchararray' 输入“数据+TrayIcon+Series”
gtk.main()gtk.main()

我应该怎么做才能让它工作?

【问题讨论】:

    标签: python gtk pygtk


    【解决方案1】:

    倒数第二个块中有两个错误。

    1. GtkWarning:gtk_tree_view_column_cell_layout_set_cell_data_func:断言 `info != NULL'

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

    2. 警告:无法根据“typedata+TrayIcon+Series”的值设置“gchararray”类型的属性“text”

      这是因为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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-08
      • 2018-12-22
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      相关资源
      最近更新 更多