1

我正在尝试将 GtkImage 添加到我的主窗口 *.ui 文件中,该文件是使用 GNOME Builder 项目模板制作的。

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk+" version="3.20"/>
  <template class="PodiumWindow" parent="GtkApplicationWindow">
    <property name="default-width">600</property>
    <property name="default-height">300</property>
    <child type="titlebar">
      <object class="GtkHeaderBar" id="headerBar">
        <property name="visible">True</property>
        <property name="show-close-button">True</property>
        <property name="title">Podium</property>
      </object>
    </child>
    <child>
    <object class="GtkImage">
          <property name="visible">True</property>
          <property name="icon-name">open-menu-symbolic</property>
          <property name="icon-size">1</property>
    </object>
    <object class="GtkLabel" id="label">
      <property name="label">Ready your pencils!</property>
      <property name="visible">True</property>
      <attributes>
        <attribute name="weight" value="bold"/>
        <attribute name="scale" value="2"/>
      </attributes>
    </object>
    </child>
  </template>
</interface>

但是图像没有出现在窗口中: 你好世界窗口

我检查了 GTK+ Inspector (Ctrl+Shift+D),在层次结构中看不到 GtkImage。有人知道我错过了什么吗?

4

2 回答 2

3

GtkWindow是 的子类GtkBin,这意味着它一次只能有一个孩子。

所以图像是先添加的,但在添加标签时再次删除。

要解决您的问题,您需要添加一个中间容器(如GtkGridGtkBox)。

于 2019-08-26T08:30:33.280 回答
0

根据 Florians 的回答,我在下面发布了我的工作 *.UI 代码:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <template class="PodiumWindow" parent="GtkApplicationWindow">
    <property name="can_focus">False</property>
    <property name="default_width">600</property>
    <property name="default_height">300</property>
    <child type="titlebar">
      <object class="GtkHeaderBar" id="headerBar">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="title">Podium</property>
        <property name="show_close_button">True</property>
      </object>
    </child>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="valign">center</property>
        <property name="vexpand">True</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkImage">
            <property name="visible">True</property>
            <property name="icon-name">open-menu-symbolic</property>
            <property name="icon-size">1</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <object class="GtkLabel" id="label">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label">Ready your pencils!</property>
            <attributes>
              <attribute name="weight" value="bold"/>
              <attribute name="scale" value="2"/>
            </attributes>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </template>
</interface>

带有图像的 HelloWorld 窗口

于 2019-08-27T07:52:21.857 回答