简体   繁体   English

将 Gtk.Label 放入 Gtk.TreeView 行

[英]Puting Gtk.Label in Gtk.TreeView row

I try to put markuped text (potentially containing italic, bold, color) in a Gtk.TreeView column, using the following example:我尝试使用以下示例将标记文本(可能包含斜体、粗体、颜色)放入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()

And I get the following error message:我收到以下错误消息:

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

And the following window rendering:以及下面的 window 渲染:

第一个窗口渲染图像

As you see, the “Label” column row is empty when I was expecting something like “ baz foo”.如您所见,当我期待“ baz foo”之类的内容时,“标签”列是空的。

As the error message said, Gtk try to find the property text in GtkLabel and doesn't find it.如错误消息所述,Gtk 尝试在GtkLabel中查找属性text但未找到。 So, I replace text into label to make the line like this column_text_1 = Gtk.TreeViewColumn("Label", renderer_text_1, label=1) .因此,我将text替换为label以使行如下column_text_1 = Gtk.TreeViewColumn("Label", renderer_text_1, label=1) But then I get the following error:但后来我收到以下错误:

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

So what is the solution to put a Label (or other Gtk's widgets) inside a TreeView ?那么将Label (或其他 Gtk 的小部件)放入TreeView的解决方案是什么?

You cannot put widgets inside a GtkTreeView, only cell renderers.不能将小部件放在 GtkTreeView 中,只能将单元格渲染器。

If you want to show markup inside a tree view column, you can use the markup property of GtkCellRendererText instead of the text property:如果要在树视图列中显示标记,可以使用GtkCellRendererTextmarkup属性而不是text属性:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM