GTK3 multiple UI files

1.3k views Asked by At

I am tying to make a login form using Python and GTK3 with the help of Anjuta. So far, I have a UI file for my login section, and a form that makes an HTTP request. The class looks like this:

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 = ''

When I fill in the correct data, the HTTP request is successful and I can see the response in the console. But what I want to do is clear the current window (UI file) and load a new one in its place. That is, without closing the actual window and opening up a new one. How do I do that?

1

There are 1 answers

6
erick2red On

Let's say you have this ui file

    <?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>

Now, you create you your window as usual:

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

If you want to change the content of the window. Then you need to destroy/hide the widget you want to hide.

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

And we're all good to go. You can load your second widget hierarchy (grid2, I mean) from the same initial ui file or from a new one. What you need to understand, is that after the Builder create the objects from the ui files, it can't modify those further.