Running helloworld2.py produces the window illustrated in 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. 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. |