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

时间:2018-03-21 06:13:49

标签: python-3.x gtk3 glade

我是Python Gtk3的新手,实际上是学习基础知识。在此代码中,我尝试使用对话框窗口在数据中添加项目,然后使其在主窗口中作为新的流程图子项显示在流程图中。问题是添加了flowbox子项,但没有标签出现在其中。我无法弄清楚原因。

请告诉我这段代码有什么问题?

#!/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>

1 个答案:

答案 0 :(得分:0)

在第33行创建后,只需添加android:orientation="vertical" ,即可看到您的小部件。您可能还对容器的label.show()函数感兴趣。