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.

Our first example uses the Tix module. Tix comes with <strong>Python</strong>!<br />

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

2<br />

3 from Tkinter import Label, Button, END<br />

4 from Tix import Tk, Control, ComboBox<br />

5<br />

6 top = Tk()<br />

7 top.tk.eval('package require Tix')<br />

8<br />

9 lb = Label(top,<br />

10 text='Animals (in pairs; min: pair, max: dozen)')<br />

11 lb.pack()<br />

12<br />

13 ct = Control(top, label='Number:',<br />

14 integer=True, max=12, min=2, value=2, step=2)<br />

15 ct.label.config(font='Helvetica -14 bold')<br />

16 ct.pack()<br />

17<br />

18 cb = ComboBox(top, label='Type:', editable=True)<br />

19 for animal in ('dog', 'cat', 'hamster', 'python'):<br />

20 cb.insert(END, animal)<br />

21 cb.pack()<br />

22<br />

23 qb = Button(top, text='QUIT',<br />

24 command=top.quit, bg='red', fg='white')<br />

25 qb.pack()<br />

26<br />

27 top.mainloop()<br />

Line-by-Line Explanation<br />

Lines 17<br />

This is all the setup code, module imports, and basic GUI infrastructure. Line 7 asserts that the Tix<br />

module is available to the application.<br />

Lines 827<br />

These lines create all the widgets: Label (lines 9-11), Control (lines 13- 16), ComboBox (lines 18-21), and<br />

quit Button (lines 23-25). The constructors and arguments for the widgets are fairly self-explanatory and<br />

do not require elaboration. Finally, we enter the main GUI event loop in line 27.<br />

19.4.2. <strong>Python</strong> MegaWidgets (PMW)<br />

Next we take a look at <strong>Python</strong> MegaWidgets as shown in Example 19.8. This module was created to<br />

address the aging Tkinter. It basically helps the extend its longevity by adding more modern widgets to<br />

the GUI palette.<br />

Example 19.8. Pmw GUI Demo (animalPmw.pyw)

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

Saved successfully!

Ooh no, something went wrong!