GTK3多个UI文件

时间:2013-09-03 16:04:03

标签: python python-2.7 gtk3 anjuta

我想在Anjuta的帮助下使用Python和GTK3创建一个登录表单。到目前为止,我有一个用于登录部分的UI文件,以及一个发出HTTP请求的表单。该课程如下:

class GUI:
    def __init__(self):
        self.builder = Gtk.Builder()
        self.builder.add_from_file(UI_FILE)
        self.builder.connect_signals(self)

        window = self.builder.get_object('window')
        window.set_title("LOGIN")

        window.show_all()
        self.username = ''
        self.password = ''

当我填写正确的数据时,HTTP请求成功,我可以在控制台中看到响应。但我想要做的是清除当前窗口(UI文件)并在其位置加载一个新窗口。也就是说,不关闭实际窗口并打开一个新窗口。我该怎么做?

1 个答案:

答案 0 :(得分:0)

假设你有这个ui文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Generated with glade 3.15.3 on Tue Sep 17 10:11:35 2013 -->
    <interface>
      <!-- interface-requires gtk+ 3.10 -->
      <object class="GtkGrid" id="grid2">
        <property name="visible">True</property>
        <property name="row_spacing">12</property>
        <property name="row_homogeneous">True</property>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="label" translatable="yes">I'm a widget from the new content</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">0</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label2">
            <property name="visible">True</property>
            <property name="label" translatable="yes">Me too</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">1</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
      </object>
      <object class="GtkWindow" id="window1">
        <child>
          <object class="GtkGrid" id="grid1">
            <property name="visible">True</property>
            <child>
              <object class="GtkButton" id="button1">
                <property name="label" translatable="yes">button</property>
                <property name="visible">True</property>
              </object>
              <packing>
                <property name="left_attach">0</property>
                <property name="top_attach">0</property>
                <property name="width">1</property>
                <property name="height">1</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="button2">
                <property name="label" translatable="yes">button</property>
                <property name="visible">True</property>
              </object>
              <packing>
                <property name="left_attach">1</property>
                <property name="top_attach">1</property>
                <property name="width">1</property>
                <property name="height">1</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="button3">
                <property name="label" translatable="yes">button</property>
                <property name="visible">True</property>
              </object>
              <packing>
                <property name="left_attach">2</property>
                <property name="top_attach">2</property>
                <property name="width">1</property>
                <property name="height">1</property>
              </packing>
            </child>
          </object>
        </child>
      </object>
    </interface>

现在,您像往常一样创建窗口:

    window1 = self.builder.get_object('window2')
    window1.show_all()

如果要更改窗口的内容。然后你需要销毁/隐藏你想隐藏的小部件。

    grid1 = self.builder.get_object('grid1')
    grid1.destroy()
    grid2 = self.builder.get_object('grid2')
    window1.add (grid2)

我们都很高兴。您可以从同一个初始ui文件或新文件加载第二个窗口小部件层次结构( grid2 ,我的意思)。您需要了解的是,在构建器从ui文件创建对象后,它无法进一步修改它们。