15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Lines 3033<br />

These lines represent our PFA magic. We use two levels of PFA. The first templatizes the Button class<br />

and the root window top. What this does is that every time we call MyButton, it will call Button (Tkinter.<br />

Button() creates a button.) with top as its first argument. We have "frozen" this into MyButton.<br />

The second level of PFA is where we use our first one, MyButton, and templatize that. We create separate<br />

button types for each of our sign categories. When users create a critical button CritButton (by calling<br />

it, e.g., CritButton()), it will then call MyButton along with the appropriate button callback and<br />

background and foreground colors, which means calling Button with top, callback, and colors. Do you<br />

see how it unwinds and goes down the layers until at the very bottom, it has the call that you would<br />

have originally had to make if this feature did not exist yet? We repeat with Warn-Button and ReguButton.<br />

Lines 3542<br />

With the setup completed, we look at our list of signs and create them. We put together a <strong>Python</strong><br />

evaluatable string consisting of the correct button name, pass in the button label as the text argument,<br />

and pack() it. If it is a critical sign, then we CAPITALIZE the button text, otherwise we titlecase it. This<br />

last bit is done in line 39, demonstrating another feature introduced in <strong>Python</strong> 2.5, the temporary<br />

operator. Then we take each button creation string and execute it with eval(), creating the buttons one<br />

at a time and resulting in the graphic seen previously. Finally, we start the GUI by entering the main<br />

event loop.<br />

This application uses several <strong>Python</strong> 2.5 features, so you will not be able to run this with an older<br />

version.<br />

19.3.6. Intermediate Tkinter Example<br />

We conclude this section with a larger example, listdir.py. This application is a directory tree traversal<br />

tool. It starts in the current directory and provides a file listing. Double-clicking on any other directory in<br />

the list causes the tool to change to the new directory as well as replace the original file listing with the<br />

files from the new directory. The source code is given as Example 19.6.<br />

Example 19.6. File System Traversal GUI (listdir.py)<br />

This slightly more advanced GUI expands on the use of widgets, adding listboxes, text<br />

entry fields, and scrollbars to our repertoire. There are also a good number of callbacks<br />

such as mouse clicks, key presses, and scrollbar action.<br />

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

2<br />

3 import os<br />

4 from time import sleep<br />

5 from Tkinter import *<br />

6<br />

7 class DirList(object):<br />

8<br />

9 def __init__(self, initdir=None):<br />

10 self.top = Tk()<br />

11 self.label = Label(self.top,<br />

12 text='Directory Lister v1.1')

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

Saved successfully!

Ooh no, something went wrong!