GUI(PyGObject)中缺少文本



我已经使用xml描述文件用pyGObject创建了一个GUI,并试图用GtkTreeView创建一个侧边栏。GtkTreeView不会在GUI的标题和子项中显示任何文本,即使我添加了一些。为什么?我该怎么修?

侧边栏应该显示以下内容:

Parent 1
    Child 1
    Child 2
    Child 3

等等。

我的代码的简短版本如下所示。

应用程序py

#!/usr/bin/env python2
from gi.repository import Gtk

class AppUI:
    def __init__(self):
        self.builder = Gtk.Builder()
        self.builder.add_from_file("app.xml")
        self.window = self.builder.get_object("main-window")
        self.sidebarStore = self.builder.get_object("sidebar-store")
        for parent in range(4):
            piter = self.sidebarStore.append(None, ['parent %i' % parent])
            for child in range(3):
                self.sidebarStore.append(piter, ['child %i of parent %i' % (child, parent)])
        self.handlers = {
            "onDeleteWindow": Gtk.main_quit,
        }
        self.builder.connect_signals(self.handlers)
        self.window.show_all()
UI = AppUI()
Gtk.main()

app.xml:UI描述

<?xml version="1.0" encoding="UTF-8"?>
<interface>
    <object class="GtkTreeStore" id="sidebar-store">
        <columns>
            <column type="gchararray"/>
        </columns>
    </object>
    <object class="GtkWindow" id="main-window">
        <property name="title"></property>
        <signal name="delete-event" handler="onDeleteWindow"/>
        <child>
            <object class="GtkBox" id="container">
                <property name="orientation">horizontal</property>
                <child>
                    <object class="GtkTreeView" id="sidebar">
                        <property name="model">sidebar-store</property>
                        <property name="headers-visible">false</property>
                        <child>
                            <object class="GtkTreeViewColumn" id="test-column">
                                <child>
                                    <object class="GtkCellRendererText" id="test-renderer"/>
                                </child>
                            </object>
                        </child>
                    </object>
                </child>
                <child>
                    <object class="GtkBox" id="right-container">
                        <property name="orientation">vertical</property>
                        <child>
                            <object class="GtkButtonBox" id="top-buttonbox">
                                <child>
                                    <object class="GtkButton" id="add-button">
                                        <property name="label">Add</property>
                                    </object>
                                </child>
                                <child>
                                    <object class="GtkButton" id="delete-button">
                                        <property name="label">Delete</property>
                                    </object>
                                </child>
                            </object>
                        </child>
                    </object>
                </child>
            </object>
        </child>
    </object>
</interface>

非常简单,您没有将任何列映射到文本呈现器。

顺便说一句,你用什么编辑器来创建UI?你是手写的吗?使用Glade,这样生活会更轻松。

在Glade中,右键单击TreeView->Edit->Hierarchy->Select Cell Renderer->映射到模型中列的Text。

以下是通过Glade编辑的XML的更正版本。最相关的部分是:

<attributes>
    <attribute name="text">0</attribute>
</attributes>

定义文本单元渲染器时。

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkTreeStore" id="sidebar-store">
    <columns>
      <!-- column-name gchararray -->
      <column type="gchararray"/>
    </columns>
  </object>
  <object class="GtkWindow" id="main-window">
    <property name="can_focus">False</property>
    <signal name="delete-event" handler="onDeleteWindow" swapped="no"/>
    <child>
      <object class="GtkBox" id="container">
        <property name="can_focus">False</property>
        <child>
          <object class="GtkTreeView" id="sidebar">
            <property name="width_request">100</property>
            <property name="can_focus">False</property>
            <property name="model">sidebar-store</property>
            <property name="headers_visible">False</property>
            <child internal-child="selection">
              <object class="GtkTreeSelection" id="treeview-selection1"/>
            </child>
            <child>
              <object class="GtkTreeViewColumn" id="test-column">
                <child>
                  <object class="GtkCellRendererText" id="test-renderer"/>
                  <attributes>
                    <attribute name="text">0</attribute>
                  </attributes>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkBox" id="right-container">
            <property name="can_focus">False</property>
            <property name="orientation">vertical</property>
            <child>
              <object class="GtkButtonBox" id="top-buttonbox">
                <property name="can_focus">False</property>
                <child>
                  <object class="GtkButton" id="add-button">
                    <property name="label">Add</property>
                    <property name="use_action_appearance">False</property>
                    <property name="can_focus">False</property>
                    <property name="receives_default">False</property>
                    <property name="use_action_appearance">False</property>
                  </object>
                  <packing>
                    <property name="expand">False</property>
                    <property name="fill">True</property>
                    <property name="position">0</property>
                  </packing>
                </child>
                <child>
                  <object class="GtkButton" id="delete-button">
                    <property name="label">Delete</property>
                    <property name="use_action_appearance">False</property>
                    <property name="can_focus">False</property>
                    <property name="receives_default">False</property>
                    <property name="use_action_appearance">False</property>
                  </object>
                  <packing>
                    <property name="expand">False</property>
                    <property name="fill">True</property>
                    <property name="position">1</property>
                  </packing>
                </child>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

相关内容

  • 没有找到相关文章

最新更新