0

我想在 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 文件)并在其位置加载一个新窗口。也就是说,无需关闭实际窗口并打开一个新窗口。我怎么做?

4

1 回答 1

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 )。您需要了解的是,在Builder从 ui 文件创建对象之后,它不能进一步修改这些对象。

于 2013-09-03T21:20:14.347 回答