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

发布于 2024-09-14 18:40:41 字数 1636 浏览 12 评论 0原文

我试图使用我找到的代码片段保留 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' 失败
col.set_cell_data_func(单元格, 获取系列标题) 警告:无法设置属性
text' 类型为 gchararray' 的值 类型data+TrayIcon+Series'
window.show_all() 警告:无法设置属性 text' 类型为gchararray' 来自值 输入“数据+TrayIcon+系列”
gtk.main()gtk.main()

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

I'w trying to keep at ListStore non-text objects using snippet I found. These are the objects:

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

And now I'm trying to make TreeView display it:

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

But it fails with errors:

GtkWarning:
gtk_tree_view_column_cell_layout_set_cell_data_func:
assertion info != NULL' failed
col.set_cell_data_func(cell,
get_series_title)
Warning: unable to set property
text'
of type gchararray' from value of
type
data+TrayIcon+Series'
window.show_all()
Warning: unable to set property text'
of type
gchararray' from value of
type `data+TrayIcon+Series'
gtk.main()gtk.main()

What should I do to make it work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾听心声的旋律 2024-09-21 18:40:41

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

  1. GtkWarning:gtk_tree_view_column_cell_layout_set_cell_data_func:断言“信息!= 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)

Two mistakes in the second-to-last block.

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

    In English, this means that the cell renderer is not in the column's list of cell renderers. You need to add the cell renderer to the column first before calling set_cell_data_func.

  2. Warning: unable to set property `text' of type `gchararray' from value of `typedata+TrayIcon+Series'

    This is because the add_attribute line causes GTK+ to try setting the cell text to a Series object, which of course fails. Just remove that line; the cell data func already takes care of setting the cell text.

In code:

col = gtk.TreeViewColumn(_("Series title"))
cell = gtk.CellRendererText()
col.pack_start(cell)
col.set_cell_data_func(cell, get_series_title)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文