[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

if name == 'main': Demo().mainloop()


This script creates the window shown in Figure 8-11 when run as a standalone program;
it’s a bar of demo buttons that simply route control back to the values of the table in
the module dialogTable when pressed.


Figure 8-11. demoDlg main window


Notice that because this script is driven by the contents of the dialogTable module’s
dictionary, we can change the set of demo buttons displayed by changing just
dialogTable (we don’t need to change any executable code in demoDlg). Also note that
the Quit button here is an attached instance of the Quitter class of the prior section
whose frame is repacked to stretch like the other buttons as needed here—it’s at least
one bit of code that you never have to write again.


This script’s class also takes care to pass any **options constructor configuration key-
word arguments on to its Frame superclass. Though not used here, this allows callers
to pass in configuration options at creation time (Demo(o=v)), instead of configuring
after the fact (d.config(o=v)). This isn’t strictly required, but it makes the demo class
work just like a normal tkinter frame widget (which is what subclassing makes it, after
all). We’ll see how this can be used to good effect later.


We’ve already seen some of the dialogs triggered by this demo bar window’s other
buttons, so I’ll just step through the new ones here. Pressing the main window’s Query
button, for example, generates the standard pop up in Figure 8-12.


This askquestion dialog looks like the askyesno we saw earlier, but actually it returns
either string "yes" or "no" (askyesno and askokcancel return True or False instead—
trivial but true). Pressing the demo bar’s Input button generates the standard ask
float dialog box shown in Figure 8-13.


Dialogs | 431
Free download pdf