Part 1b

Simple Glade3 GUI

The Python

The python code was short and sweet so lets examine what each part is for.

Executable Path

The #!/usr/bin/env python line will use the installed python that appears in the $PATH. This is how you make the file executable without having to type in python then the file name to run the file.

If you opened up a terminal and changed directories to the location of your python file you would use ./tutorial-1.py to run the file. The ./ means execute the file in this directory, without this Linux will search the normal installed locations for an executable named tutorial-1.py.

An alternate method is to use python tutorial-1.py to run the program. Using this method the file need not be set to executable and doesn’t need the #!/usr/bin/env python line.

Set the path to Python
#!/usr/bin/env python

Import Libraries

Next we need to import some libraries and make sure we are using pygtk2.0. If gtk failed to import the except: would be run and the print statement would print to the terminal the error message then execution of the program would abort.

Import the Gtk Library
import gtk

Main Class

Think of a class as a blueprint. It doesn’t do anything itself, it simply describes how to make something. You can create objects from a class, known technically as an instance of the class.

Next we create the main class that is called when we run the program. It can be named just about anything but reserved key words in Python. If your an old movie fan you know what Buglump means…

The Buglump Class
# we can call it just about anything we want
class Buglump:

# This first define is for our on_window1_destroy signal we created in the
# Glade designer. The print message does just that and prints to the terminal
# which can be useful for debugging. The 'object' if you remember is the signal
# class we picked from GtkObject.
  def on_window1_destroy(self, object, data=None):
    print "quit with cancel"
    gtk.main_quit()

# This is the same as above but for our menu item.
  def on_gtk_quit_activate(self, menuitem, data=None):
    print "quit from menu"
    gtk.main_quit()

# This is our init part where we connect the signals
  def __init__(self):
    self.gladefile = "tutorial-1.glade" # store the file name
    self.builder = gtk.Builder() # create an instance of the gtk.Builder
    self.builder.add_from_file(self.gladefile) # add the xml file to the Builder

# This line does the magic of connecting the signals created in the Glade3
# builder to our defines above. You must have one def for each signal if
# you use this line to connect the signals.
    self.builder.connect_signals(self)

    self.window = self.builder.get_object("window1") # This gets the 'window1' object
    self.window.show() # this shows the 'window1' object
Create an Instance
# If this is run stand alone execute the following after the 'if'
# If this class is imported into another program the code after the 'if' will
# not run. This makes the code more flexible.
if __name__ == "__main__":
  main = Buglump() # create an instance of our class
  gtk.main() # run the darn thing

The Python

The following code is the complete python file. In the same directory as the glade1.glade file was saved save the following python code as glade1.py.

glade1.py
#!/usr/bin/env python

import gtk

class Buglump:

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

  def on_gtk_quit_activate(self, menuitem, data=None):
    print "quit from menu"
    gtk.main_quit()

  def __init__(self):
    self.gladefile = "tutorial-1.glade"
    self.builder = gtk.Builder()
    self.builder.add_from_file(self.gladefile)
    self.builder.connect_signals(self)
    self.window = self.builder.get_object("window1")
    self.window.show()

if __name__ == "__main__":
  main = Buglump()
  gtk.main()

View the Results

Now open up a terminal and cd to the directory where your files are. Type ls to see the contents of the directory. You should see your file names in black.

images/glade-01-07.png

Now type python glade1.py or ./glade1.py and our screen pops up…

images/glade-01-08.png

Not much to look at but if you select Quit from the file menu it magically disappears and in the terminal a message is printed.

images/glade-01-09.png

That’s all for this one and we didn’t even say Hello World