Part 9d

Starting the UI

Running the UI

Download main.glade and extract it in the /lib subdirectory.

Now we have to add some things to the main.py file to open up and show the Gtk UI. First we need to import PyGtk and Gtk, so we add the following to the start of the main.py.

Import PyGtk and Gtk
#!/usr/bin/env python

try:
  import pygtk
  pygtk.require('2.0')
except:
  pass
try:
  import gtk
  import gtk.glade
except:
  print('GTK not available')
  sys.exit(1)

Next we change the init section of the main class to add the glade builder and show the main window

class main:
  def __init__(self):
    # create an instance of the Builder
    self.builder = gtk.Builder()
    self.builder.add_from_file('lib/main.glade')
    self.builder.connect_signals(self)

    # setup the main window
    self.window = self.builder.get_object("window1")
    self.window.set_title("ComboBoxes")
    self.window.show_all()

Next we need to add the modules that handle the signals from the radio buttons and handle the closing of the window. When self.builder.connect_signals(self) runs it expects to find a module for every Signal Handler defined in the Glade file.

The widget.get_active() returns a true or false based on if it is the selected radio button. If we didn’t do this then every time a radio button in the same group is changed the toggled signal will be emitted so the module would run.

If you open the main.glade file and look in the General tab for Slimy you will see that it is part of the radiobutton1 group. Every radio button in a group will only allow one radio button to be selected. So when you select one the the previous selected radio button will de-select.

  def on_radiobutton1_toggled(self, widget):
    if widget.get_active():
      print '1'

  def on_radiobutton2_toggled(self, widget):
    if widget.get_active():
      print '2'

  def on_window1_destroy(self, object, data=None):
    print "quit with cancel"
    gtk.main_quit()

And last we need to add the gtk.main() to show the UI window

Complete main.py
#!/usr/bin/env python

try:
  import pygtk
  pygtk.require('2.0')
except:
  pass
try:
  import gtk
  import gtk.glade
except:
  print('GTK not available')
  sys.exit(1)

import lib.myclass as mc

class main:
  def __init__(self):
    # create an instance of the Builder
    self.builder = gtk.Builder()
    self.builder.add_from_file('lib/main.glade')
    self.builder.connect_signals(self)

    # setup the main window
    self.window = self.builder.get_object("window1")
    self.window.set_title("ComboBoxes")
    self.window.show_all()

  def on_radiobutton1_toggled(self, widget):
    if widget.get_active():
      print '1'

  def on_radiobutton2_toggled(self, widget):
    if widget.get_active():
      print '2'

  def on_window1_destroy(self, object, data=None):
    print "quit with cancel"
    gtk.main_quit()

main = main()
gtk.main()

When we run main.py we will see the following UI

images/glade-09-03.png

Pressing the Furry or Slimy radio buttons will run the module and print to the terminal.