python - 将模型中的字段合并到一个 Gtk.TreeViewColumn 中

标签 python python-3.x gtk pygobject gtktreeview

我不确定,但我认为可以使用多个 Gtk.CellRendererGtk.TreeViewColumn当使用pack_start()时.

但我无法运行它,也不知道出了什么问题。此示例代码中的 TreeView 是空的。

#!/usr/bin/env python3
import random
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GLib

class TreeView(Gtk.TreeView):
    def __init__(self):
        # model
        self.model = Gtk.ListStore.new([int, int])
        for i in range(5):
            self.model.append([i, (i*10)])

        # view
        Gtk.TreeView.__init__(self, self.model)

        col = Gtk.TreeViewColumn.new()
        col.set_title('two model fields')

        self.rendererA = Gtk.CellRendererText()
        col.add_attribute(self.rendererA, 'text', 0)
        col.pack_end(self.rendererA, True)

        self.rendererB = Gtk.CellRendererText()
        col.add_attribute(self.rendererB, 'text', 1)
        col.pack_end(self.rendererB, True)

        self.append_column(col)

if __name__ == '__main__':
    win = Gtk.Window.new(0)
    win.view = TreeView()
    win.add(win.view)
    win.connect('destroy', Gtk.main_quit)
    win.show_all()
    Gtk.main()

Gtk 警告是

(_col.py:22411): Gtk-CRITICAL **: 13:23:09.919: gtk_cell_area_attribute_connect: assertion 'gtk_cell_area_has_renderer (area, renderer)' failed

最佳答案

除了函数顺序之外,你做的一切都是正确的。首先打包,然后添加属性。

关于python - 将模型中的字段合并到一个 Gtk.TreeViewColumn 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50271879/

相关文章:

python - 基于列名在 pandas 数据框的 lambda 表达式上使用 if else 语句

c - 带有 XML 接口(interface)的基本 UI Gtk

python - 如何在 Python 中更改 GtkTreeView 样式?

python - 每 X 分钟运行一个函数 - Python

python - 如何在 Python 网站中查找未使用的代码?

python - cherrypy.request.body.read() 错误

python - 如何在同一应用程序中运行 python Flask 和 TCP 服务器

用于从 image-net.org 下载图像以进行 haar 级联训练的 python 代码

python - pg8000 和 sqlalchemy 查询会引发异常,但仅有时发生

python - 设置 Python 类属性的正确方法