3.2. An Upgraded Hello World

Let's take a look at a slightly improved helloworld (helloworld2.py)with better examples of callbacks. This will also introduce us to our next topic, packing widgets.
    1   #!/usr/bin/env python
    3   # example helloworld2.py
    5   import gtk
    7   class HelloWorld2:
    9       # Our new improved callback.  The data passed to this method
   10       # is printed to stdout.
   11       def callback(self, widget, data):
   12           print "Hello again - %s was pressed" % data
   14       # another callback
   15       def delete_event(self, widget, event, data=None):
   16           gtk.mainquit()
   17           return gtk.FALSE
   19       def __init__(self):
   20           # Create a new window
   21           self.window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
   23           # This is a new call, which just sets the title of our
   24           # new window to "Hello Buttons!"
   25           self.window.set_title("Hello Buttons!")
   27           # Here we just set a handler for delete_event that immediately
   28           # exits GTK.
   29           self.window.connect("delete_event", self.delete_event)
   31           # Sets the border width of the window.
   32           self.window.set_border_width(10)
   34           # We create a box to pack widgets into.  This is described in detail
   35           # in the "packing" section. The box is not really visible, it
   36           # is just used as a tool to arrange widgets.
   37           self.box1 = gtk.GtkHBox(gtk.FALSE, 0)
   39           # Put the box into the main window.
   40           self.window.add(self.box1)
   42           # Creates a new button with the label "Button 1".
   43           self.button1 = gtk.GtkButton("Button 1")
   45           # Now when the button is clicked, we call the "callback" method
   46           # with a pointer to "button 1" as its argument
   47           self.button1.connect("clicked", self.callback, "button 1")
   49           # Instead of add(), we pack this button into the invisible
   50           # box, which has been packed into the window.
   51           self.box1.pack_start(self.button1, gtk.TRUE, gtk.TRUE, 0)
   53           # Always remember this step, this tells GTK that our preparation for
   54           # this button is complete, and it can now be displayed.
   55           self.button1.show()
   57           # Do these same steps again to create a second button
   58           self.button2 = gtk.GtkButton("Button 2")
   60           # Call the same callback method with a different argument,
   61           # passing a pointer to "button 2" instead.
   62           self.button2.connect("clicked", self.callback, "button 2")
   64           self.box1.pack_start(self.button2, gtk.TRUE, gtk.TRUE, 0)
   66           # The order in which we show the buttons is not really important, but I
   67           # recommend showing the window last, so it all pops up at once.
   68           self.button2.show()
   69           self.box1.show()
   70           self.window.show()
   72   def main():
   73       gtk.mainloop()
   75   if __name__ == "__main__":
   76       hello = HelloWorld2()
   77       main()

Running helloworld2.py produces the window illustrated in Figure 3.1.

Figure 3.1 Upgraded Hello World Example

You'll notice this time there is no easy way to exit the program, you have to use your window manager or command line to kill it. A good exercise for the reader would be to insert a third "Quit" button that will exit the program. You may also wish to play with the options to pack_start() while reading the next section. Try resizing the window, and observe the behavior.

Just as a side note, there is another useful define for GtkWindow() - WINDOW_DIALOG. This interacts with the window manager a little differently and should be used for transient windows.

A short commentary on the code differences from the first helloworld program is in order.

As noted above there is no "destroy" event handler in the upgraded helloworld.

Lines 11-12 define a callback method which is similar to the hello() callback in the first helloworld. The difference is that the callback prints a message including data passed in.

Line 25 sets a title string to be used on the titlebar of the window (see Figure 3.1).

Line 37 creates a horizontal box (GtkHBox) to hold the two buttons that are created in lines 43 and 58. Line 40 adds the horizontal box to the window container.

Lines 47 and 62 connect the callback() method to the "clicked" signal of the buttons. Each button sets up a different string to be passed to the callback() method when invoked.

Lines 51 and 64 pack the buttons into the horizontal box. Lines 55 and 68 ask GTK to display the buttons.

Lines 69-70 ask GTK to display the box and the window respectively.