2.4. Stepping Through Hello World

Now that we know the theory behind this, let's clarify by walking through the example helloworld.py program.

Lines 9-76 define the HelloWorld class that contains all the callbacks as object methods and the object instance initialization method. Let's examine the callback methods.

Lines 13-14 define the hello() callback method that will be called when the button is "clicked". When called the method, prints "Hello World" to the console. We ignore the object instance, the widget and the data parameters in this example, but most callbacks use them. The data is defined with a default value of None because PyGTK will not pass a data value if it is not included in the connect() call; this would trigger an error since the callback is expecting three parameters and may receive only two. Defining a default value of None allows the callback to be called with two or three parameters without error. In this case the data parameter could have been left out since the hello() method will always be called with just two parameters (never called with user data). The next example will use the data argument to tell us which button was pressed.

  def hello(self, widget, data=None):
      print "Hello World"

The next callback (lines 16-26) is a bit special. The "delete_event" occurs when the window manager sends this event to the application. We have a choice here as to what to do about these events. We can ignore them, make some sort of response, or simply quit the application.

The value you return in this callback lets GTK+ know what action to take. By returning TRUE, we let it know that we don't want to have the "destroy" signal emitted, keeping our application running. By returning FALSE, we ask that "destroy" be emitted, which in turn will call our "destroy" signal handler. Note the comments have been removed for clarity.

  def delete_event(widget, event, data=None):
      print "delete event occurred"
      return False

The destroy() callback method (lines 28-30) causes the program to quit by calling gtk.main_quit() . This function tells GTK+ that it is to exit from gtk.main() when control is returned to it.

  def destroy(widget, data=None):
      print "destroy signal occurred"
      gtk.main_quit()

Lines 32-71 define the HelloWorld object instance initialization method __init__() that creates the window and widgets used by the program.

Line 34 creates a new window, but it is not displayed until we direct GTK+ to show the window near the end of our program. The window reference is saved in an object instance attribute (self.window) for later access.

    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

Lines 41 and 46 illustrate two examples of connecting a signal handler to an object, in this case, the window. Here, the "delete_event" and "destroy" signals are caught. The first is emitted when we use the window manager to kill the window, or when we use the GtkWidget destroy() method call. The second is emitted when, in the "delete_event" handler, we return FALSE.

    self.window.connect("delete_event", self.delete_event)
    self.window.connect("destroy", self.destroy)

Line 49 sets an attribute of a container object (in this case the window) to have a blank area along the inside of it 10 pixels wide where no widgets will be placed. There are other similar methods that we will look at in Chapter 18, Setting Widget Attributes

    self.window.set_border_width(10)

Line 52 creates a new button and saves a reference to it in self.button. The button will have the label "Hello World" when displayed.

    self.button = gtk.Button("Hello World")

In line 57 we attach a signal handler to the button so when it emits the "clicked" signal, our hello() callback method is called. We are not passing any data to hello() so we just pass None as the data. Obviously, the "clicked" signal is emitted when we click the button with our mouse pointer. The user data parameter value None is not required and could be removed. The callback would then be called with one less parameter.

    self.button.connect("clicked", self.hello, None)

We are also going to use this button to exit our program. Line 62 illustrates how the "destroy" signal may come from either the window manager, or from our program. When the button is "clicked", same as above, it calls the hello() callback first, and then the following one in the order they are set up. You may have as many callbacks as you need, and all will be executed in the order you connected them.

Since we want to use the GtkWidget destroy() method that accepts one argument (the widget to be destroyed - in this case the window), we use the connect_object() method and pass it the reference to the window. The connect_object() method arranges to pass the window as the first callback argument instead of the button.

When the gtk.Widget destroy() method is called it will cause the "destroy" signal to be emitted from the window which will in turn cause the HelloWorld destroy() method to be called to end the program.

    self.button.connect_object("clicked", gtk.Widget.destroy, self.window)

Line 65 is a packing call, which will be explained in depth later on in Chapter 4, Packing Widgets . But it is fairly easy to understand. It simply tells GTK+ that the button is to be placed in the window where it will be displayed. Note that a GTK+ container can only contain one widget. There are other widgets, described later, that are designed to layout multiple widgets in various ways.

    self.window.add(self.button)

Now we have everything set up the way we want it to be. With all the signal handlers in place, and the button placed in the window where it should be, we ask GTK+ (lines 66 and 69) to "show" the widgets on the screen. The window widget is shown last so the whole window will pop up at once rather than seeing the window pop up, and then the button forming inside of it. Although with such a simple example, you'd never notice.

    self.button.show()

    self.window.show()

Widgets also have a hide() that is the opposite of show(). It doesn't actually destroy the widget, but it removes the widget renderining from your display. This can be reversed with another show() call.

Lines 73-75 define the main() method which calls the gtk.main() function

    def main(self):
        gtk.main()

Lines 80-82 allow the program to run automatically if called directly or as an argument of the python interpreter. Line 81 creates an instance of the HelloWorld class and saves a reference to it in the hello variable. Line 82 calls the HelloWorld class main() method to start the GTK+ event processing loop.

    if __name__ == "__main__":
        hello = HelloWorld()
        hello.main()

Now, when we click the mouse button on a GTK+ button, the widget emits a "clicked" signal. In order for us to use this information, our program sets up a signal handler to catch that signal, which dispatches the function of our choice. In our example, when the button we created is "clicked", the hello() method is called with the None argument, and then the next handler for this signal is called. The next handler calls the widget destroy() function with the window as its argument thereby causing the window to emit the "destroy" signal, which is caught, and calls our HelloWorld destroy() method

Another course of events is to use the window manager to kill the window, which will cause the "delete_event" to be emitted. This will call our "delete_event" handler. If we return TRUE here, the window will be left as is and nothing will happen. Returning FALSE will cause GTK+ to emit the "destroy" signal that causes the HelloWorld "destroy" callback to be called, exiting GTK.