GUI中缺少文本(PyGObject)

时间:2013-02-20 07:50:01

标签: python user-interface gtk gtk3 pygobject

我使用xml描述文件使用pyGObject创建了一个GUI,我正在尝试创建一个带有GtkTreeView的侧边栏。 GtkTreeView在我的GUI中的标题和子项中不显示任何文本,即使我添加了一些。这是为什么?我该如何解决?

侧边栏应显示以下内容:

Parent 1
    Child 1
    Child 2
    Child 3

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

App.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:用户界面说明

<?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>

1 个答案:

答案 0 :(得分:2)

很简单,您没有将任何列映射到文本渲染器。

BTW,您使用什么编辑器来创建UI?你是手工写的吗?使用Glade,生活更轻松。

在Glade中,右键单击TreeView - &gt;编辑 - &gt;层次结构 - &gt;选择单元格渲染器 - &gt;文本映射到模型中的列。

以下是通过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>