Gtk3 - Python3 - 如何在添加的 flowbox 子项中显示标签?

Gtk3 - Python3 - How to display labels in added flowbox child?

我是 Python Gtk3 的新手,正在学习基础知识。在此代码中,我尝试使用对话框 window 在数据中添加一个项目,然后使其作为主 window 中的新流盒子元素出现在流盒中。问题是添加了 flowbox child 但其中没有标签出现。我不明白为什么。

你能告诉我这段代码有什么问题吗?

#!/usr/bin/python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

# MAIN WINDOW #
class PyApp:
    def __init__(self):
        builder = Gtk.Builder()
        builder.add_from_file("ui.glade")

        dic = {"on_quit" : Gtk.main_quit,"on_add" : self.on_add}
        builder.connect_signals(dic)

        self.obj = builder.get_object

        self.items = self.getItems()

        self.box = self.obj("flowbox")
        if len(self.items) > 0:
            for item in self.items:
                self.box.add(Gtk.Label(item[0])) # newItem[0] for item id

        self.obj('main').show_all()

    def on_add(self, widget):
        """Called when the user clicks on the add button"""
        itemDlg = itemDialog();     
        result,newItem = itemDlg.run()
        if (result == 0):
            print('*** Adding new item in items data ***')
            self.items.append([newItem.id,newItem.name])
            label = Gtk.Label(newItem.id)
            self.box.add(label)


    def getItems(self):
        items = [["id1", "name1"],["id2", "name2"],["id3", "name3"]]
        return items

# ADD DIALOG #
class itemDialog:
    def __init__(self, id="", name=""):
        self.dlg = app.obj("add")
        self.item = Item(id,name)

    def run(self):
        self.result = self.dlg.run()

        #get the value of the entry fields
        self.item.id = app.obj('input_id').get_text()
        self.item.name = app.obj('input_name').get_text()

        self.dlg.hide()
        #return the result and the item
        return self.result,self.item

class Item:
    """This class represents all item information"""
    def __init__(self, id="",name=""):
        self.id = id
        self.name = name

    def getItem(self):
        return [self.id, self.name] 

if __name__ == "__main__":
    app=PyApp()
    Gtk.main()

还有空地文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="main">
    <property name="can_focus">False</property>
    <property name="default_width">600</property>
    <property name="default_height">500</property>
    <signal name="destroy" handler="on_quit" swapped="no"/>
    <child>
      <object class="GtkBox" id="box">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="orientation">vertical</property>
    <child>
      <object class="GtkToolbar" id="toolbar">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkToolButton" id="btn_add">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="use_underline">True</property>
            <property name="icon_name">list-add</property>
            <signal name="clicked" handler="on_add" swapped="no"/>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="homogeneous">True</property>
          </packing>
        </child>
        <child>
          <object class="GtkSeparatorToolItem" id="sep">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="homogeneous">False</property>
          </packing>
        </child>
        <child>
          <object class="GtkToolButton" id="btn_quit">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="can_default">True</property>
            <property name="tooltip_text" translatable="yes">Quitter</property>
            <property name="use_underline">True</property>
            <property name="icon_name">application-exit</property>
            <signal name="clicked" handler="on_quit" swapped="no"/>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="homogeneous">True</property>
          </packing>
        </child>
      </object>
      <packing>
        <property name="expand">False</property>
        <property name="fill">True</property>
        <property name="position">0</property>
      </packing>
    </child>
    <child>
      <object class="GtkFlowBox" id="flowbox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="min_children_per_line">3</property>
        <property name="max_children_per_line">3</property>
      </object>
      <packing>
        <property name="expand">True</property>
        <property name="fill">True</property>
        <property name="position">2</property>
      </packing>
    </child>
      </object>
    </child>
    <child type="titlebar">
      <placeholder/>
    </child>
  </object>
  <object class="GtkDialog" id="add">
    <property name="can_focus">False</property>
    <property name="resizable">False</property>
    <property name="type_hint">dialog</property>
    <property name="urgency_hint">True</property>
    <property name="transient_for">main</property>
    <property name="attached_to">main</property>
    <child internal-child="vbox">
      <object class="GtkBox">
    <property name="can_focus">False</property>
    <property name="margin_left">2</property>
    <property name="margin_right">2</property>
    <property name="margin_top">2</property>
    <property name="margin_bottom">2</property>
    <property name="orientation">vertical</property>
    <property name="spacing">2</property>
    <child internal-child="action_area">
      <object class="GtkButtonBox">
        <property name="can_focus">False</property>
        <property name="layout_style">end</property>
        <child>
          <object class="GtkButton" id="btn-cancel">
            <property name="label">gtk-undo</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="use_stock">True</property>
            <property name="always_show_image">True</property>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="btn-vali">
            <property name="label">gtk-add</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="use_stock">True</property>
            <property name="always_show_image">True</property>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
      <packing>
        <property name="expand">False</property>
        <property name="fill">False</property>
        <property name="position">0</property>
      </packing>
    </child>
    <child>
      <object class="GtkGrid">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="margin_top">20</property>
        <property name="margin_bottom">20</property>
        <child>
          <object class="GtkLabel" id="label_id">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="halign">start</property>
            <property name="margin_left">5</property>
            <property name="margin_right">5</property>
            <property name="margin_top">5</property>
            <property name="margin_bottom">5</property>
            <property name="label" translatable="yes">Id
</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry" id="input_id">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="margin_left">5</property>
            <property name="margin_right">5</property>
            <property name="margin_top">5</property>
            <property name="margin_bottom">5</property>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label_name">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="halign">start</property>
            <property name="margin_left">5</property>
            <property name="margin_right">5</property>
            <property name="margin_top">5</property>
            <property name="margin_bottom">5</property>
            <property name="label" translatable="yes">Name</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry" id="input_name">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="margin_left">5</property>
            <property name="margin_right">5</property>
            <property name="margin_top">5</property>
            <property name="margin_bottom">5</property>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
      </object>
      <packing>
        <property name="expand">False</property>
        <property name="fill">True</property>
        <property name="position">1</property>
      </packing>
    </child>
      </object>
    </child>
    <action-widgets>
      <action-widget response="-6">btn-cancel</action-widget>
      <action-widget response="0">btn-vali</action-widget>
    </action-widgets>
  </object>
</interface>

只需在第 33 行创建后添加 label.show(),您的小部件就会可见。您可能还对容器的 show_all() 函数感兴趣。