15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Lines 537<br />

Here we instantiate a Frame class (lines 5-8), of which the sole member is the constructor. This method's<br />

only purpose in life is to create our widgets. Inside the frame, we have a Panel. Inside the panel we use<br />

a BoxSizer to contain and layout all of our widgets (lines 10, 36), which consist of a Label (lines 12-14),<br />

SpinCtrl (lines 16-20), ComboBox (lines 22-27), and quit Button (lines 29-34).<br />

We have to manually add Labels to the SpinCtrl and ComboBox widgets because they apparently do not<br />

come with them. Once we have them all, we add them to the sizer, set the sizer to our panel, and lay<br />

everything out. On line 10, you will note that the sizer is vertically oriented, meaning that our widgets<br />

will be placed top to bottom.<br />

One weakness of the SpinCtrl widget is that it does not support "step" functionality. With the other<br />

three examples, we are able to click an arrow selector and have it increment or decrement by units of<br />

two, but that is not possible with this widget.<br />

Lines 3951<br />

Our application class instantiates the Frame object we just designed, renders it to the screen, and sets it<br />

as the top-most window of our application. Finally, the setup lines just instantiate our GUI application<br />

and start it running.<br />

19.4.4. GTK+ and PyGTK<br />

Finally, we have the PyGTK version, which is quite similar to the wx<strong>Python</strong> GUI (See Example 19.10).<br />

The biggest difference is that we use only one class, and it seems more tedious to set the foreground<br />

and background colors of objects, buttons in particular.<br />

Example 19.10. PyGTK GUI Demo (animalGtk.pyw)<br />

Our final example uses PyGTK (and GTK+). Like the wx<strong>Python</strong> example, this one also uses<br />

a class for our application. It is interesting to note how similar yet different all of our GUI<br />

applications are. This is not surprising and allows programmers to switch between toolkits<br />

with relative ease.<br />

1 #!/usr/bin/env python<br />

2<br />

3 import pygtk<br />

4 pygtk.require('2.0')<br />

5 import gtk<br />

6 import pango<br />

7<br />

8 class GTKapp(object):<br />

9 def __init__(self):<br />

10 top = gtk.Window(gtk.WINDOW_TOPLEVEL)<br />

11 top.connect("delete_event", gtk.main_quit)<br />

12 top.connect("destroy", gtk.main_quit)<br />

13 box = gtk.VBox(False, 0)<br />

14 lb = gtk.Label(

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!