3.2. An Upgraded Hello World

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

Running helloworld2.py produces the window illustrated in Figure 3.1, “Upgraded Hello World Example”.

Figure 3.1. Upgraded Hello World Example

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.

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 13-14 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 27 sets a title string to be used on the titlebar of the window (see Figure 3.1, “Upgraded Hello World Example”).

Line 39 creates a horizontal box (gtk.HBox) to hold the two buttons that are created in lines 45 and 60. Line 42 adds the horizontal box to the window container.

Lines 49 and 64 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 53 and 66 pack the buttons into the horizontal box. Lines 57 and 70 ask GTK to display the buttons.

Lines 71-72 ask GTK to display the box and the window respectively.