Gtk.TreeView行中的Puting Gtk.Label

Puting Gtk.Label in Gtk.TreeView row

提问人:fauve 提问时间:5/8/2020 更新时间:5/8/2020 访问量:455

问:

我尝试将标记文本(可能包含斜体、粗体、颜色)放在列中,使用以下示例:Gtk.TreeView

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk


class CellRendererLabelWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Gtk.Label in Gtk.ListStore Example")


        # Creating the ListStore object
        self.liststore = Gtk.ListStore(str, Gtk.Label)

        # Making the ListStore Model
        label=Gtk.Label()
        label.set_markup("<b>baz</b> foo")
        self.liststore.append(["bar", label])

        treeview = Gtk.TreeView(model=self.liststore)

        # Preparing the first column only plain text
        renderer_text_0 = Gtk.CellRendererText()
        column_text_0 = Gtk.TreeViewColumn("Text", renderer_text_0, text=0)
        treeview.append_column(column_text_0)

        # Preparing the second column with label
        renderer_text_1 = Gtk.CellRendererText()
        column_text_1 = Gtk.TreeViewColumn("Label", renderer_text_1, text=1)
        treeview.append_column(column_text_1)


        self.add(treeview)


win = CellRendererLabelWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

我收到以下错误消息:

GtkLabelInGtkList.py:43: Warning: unable to set property 'text' of type 'gchararray' from value of type 'GtkLabel'
  win.show_all()
GtkLabelInGtkList.py:44: Warning: unable to set property 'text' of type 'gchararray' from value of type 'GtkLabel'
  Gtk.main()

以及以下窗口渲染:

First window render image

如您所见,当我期待类似“baz foo”的内容时,“Label”列行是空的。

正如错误消息所说,Gtk尝试在其中找到该属性,但没有找到它。所以,我把 into 做成这样的行。但随后我收到以下错误:textGtkLabeltextlabelcolumn_text_1 = Gtk.TreeViewColumn("Label", renderer_text_1, label=1)

(GtkLabelInGtkList.py:10013): Gtk-WARNING **: Cannot connect attribute 'label' for cell renderer class 'GtkCellRendererText' since attribute does not exist

那么,将一个(或其他Gtk的小部件)放在一个LabelTreeView

蟒蛇 pygtk gtktreeview

评论


答:

1赞 ebassi 5/8/2020 #1

你不能把小部件放在GtkTreeView里,只能把单元格渲染器放进去。

如果要在树视图列中显示标记,可以使用 的 markup 属性来代替该属性:GtkCellRendererTexttext

self.liststore = Gtk.ListStore(str, str)
self.liststore.append(["bar", "<b>baz</b> foo"])

# ...

column_text_0 = Gtk.TreeViewColumn("Text", renderer_text_0, text=0)
column_text_1 = Gtk.TreeViewColumn("Markup", renderer_text_1, markup=1)