Search code examples
pythongtkglade

How to modify GUI created with glade using python code


i just started learning Glade with python , I create a simple glade file and i have added several buttons , i have also added a add button, and i want the add button to add a new button to "mainBox" object which is the child of the "window1", i am using the below given python code but it is not working as it only prints done but does not add any button to the GUI , What am i doing wrong? Thanks

  <?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <property name="default_width">800</property>
    <property name="default_height">800</property>
    <signal name="destroy" handler="on_destroy_clicked" swapped="no"/>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkBox">
        <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="GtkMenuToolButton">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="is_important">True</property>
                <property name="label" translatable="yes">Back</property>
                <property name="use_underline">True</property>
                <property name="stock_id">gtk-go-back</property>
                <signal name="show-menu" handler="on_back_clicked" swapped="no"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="homogeneous">True</property>
              </packing>
            </child>
            <child>
              <object class="GtkMenuToolButton">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="is_important">True</property>
                <property name="label" translatable="yes">Forward</property>
                <property name="use_underline">True</property>
                <property name="stock_id">gtk-sort-descending</property>
                <signal name="show-menu" handler="on_forward_clicked" swapped="no"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="homogeneous">True</property>
              </packing>
            </child>
            <child>
              <object class="GtkSeparatorToolItem">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="tooltip_text" translatable="yes">&lt;separator&gt;</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="homogeneous">True</property>
              </packing>
            </child>
            <child>
              <object class="GtkToolButton">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="tooltip_text" translatable="yes">Refresh</property>
                <property name="is_important">True</property>
                <property name="label" translatable="yes">Refresh</property>
                <property name="use_underline">True</property>
                <property name="stock_id">gtk-convert</property>
                <signal name="clicked" handler="on_refresh_clicked" swapped="no"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="homogeneous">True</property>
              </packing>
            </child>
            <child>
              <object class="GtkToolButton">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="tooltip_text" translatable="yes">Home</property>
                <property name="is_important">True</property>
                <property name="label" translatable="yes">Home</property>
                <property name="use_underline">True</property>
                <property name="stock_id">gtk-disconnect</property>
                <signal name="clicked" handler="on_home_clicked" swapped="no"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="homogeneous">True</property>
              </packing>
            </child>
            <child>
              <object class="GtkToolButton">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="is_important">True</property>
                <property name="label" translatable="yes">Add</property>
                <property name="use_underline">True</property>
                <property name="stock_id">gtk-add</property>
                <signal name="clicked" handler="on_add_clicked" swapped="no"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="homogeneous">True</property>
              </packing>
            </child>
            <child>
              <object class="GtkSeparatorToolItem">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="homogeneous">True</property>
              </packing>
            </child>
            <child>
              <object class="GtkToolButton">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="tooltip_text" translatable="yes">Delete</property>
                <property name="is_important">True</property>
                <property name="label" translatable="yes">Delete</property>
                <property name="use_underline">True</property>
                <property name="stock_id">gtk-dialog-error</property>
                <signal name="clicked" handler="on_delete_clicked" swapped="no"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="homogeneous">True</property>
              </packing>
            </child>
            <child>
              <object class="GtkToolButton">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="is_important">True</property>
                <property name="label" translatable="yes">Information</property>
                <property name="use_underline">True</property>
                <property name="stock_id">gtk-justify-fill</property>
                <signal name="clicked" handler="on_information_clicked" 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="GtkBox">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="margin_bottom">1</property>
            <property name="spacing">2</property>
            <child>
              <object class="GtkLabel">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">Current Location</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkEntry">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="margin_right">1</property>
                <property name="input_purpose">url</property>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
            <child>
              <object class="GtkImage">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="stock">gtk-execute</property>
                <property name="icon_size">3</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">2</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkBox" id="boxMain">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="homogeneous">True</property>
            <child>
              <object class="GtkButton">
                <property name="label" translatable="yes">button1</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton">
                <property name="label" translatable="yes">button2</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
            <child>
              <placeholder/>
            </child>
            <child>
              <placeholder/>
            </child>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

So , the thing i want to do with add button is to create another button in boxMain object so , i tried this code

import gi
gi.require_version('Gtk','3.0')
from gi.repository import Gtk

builder = Gtk.Builder()
builder.add_from_file("test.glade")
box = builder.get_object("boxMain")


class Handler:
    def on_back_clicked(self, button):
        pass
    def on_destroy_clicked(self, button):
        Gtk.main_quit()
    def on_forward_clicked(self, button):
        pass
    def on_refresh_clicked(self, button):
        pass
    def on_home_clicked(self, button):
        pass
    def on_information_clicked(self, button):
        pass
    def on_delete_clicked(self, button):
        pass
    def on_add_clicked(self, button):
        button1 = Gtk.Button()
        button1.set_label("Button")
        box.pack_start(button1,True,True,0)
        print("done")


builder.connect_signals(Handler())
window = builder.get_object("window1")

window.show_all()

Gtk.main()

but this code is only printing done and not adding any button to the GUI , can anybody help? Thanks


Solution

  • You are not showing your button with button1.show().

    As an aside, a better way of writing your code would be:

    import gi
    gi.require_version('Gtk','3.0')
    from gi.repository import Gtk
    
    
    
    
    class GUI:
        def __init__ (self):
    
            self.builder = Gtk.Builder()
            self.builder.add_from_file("test.glade")
            self.builder.connect_signals(self)
            window = self.builder.get_object("window1")
            window.show_all()
    
        def on_back_clicked(self, button):
            pass
        def on_destroy_clicked(self, button):
            Gtk.main_quit()
        def on_forward_clicked(self, button):
            pass
        def on_refresh_clicked(self, button):
            pass
        def on_home_clicked(self, button):
            pass
        def on_information_clicked(self, button):
            pass
        def on_delete_clicked(self, button):
            pass
        def on_add_clicked(self, button):
            box = self.builder.get_object("boxMain")
            button1 = Gtk.Button()
            button1.set_label("Button")
            box.pack_start(button1,True,True,0)
            button1.show()
            print("done")
    
    
    GUI()
    Gtk.main()