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

(yzsuai) #1

classes I teach to remind myself and my students when it’s time to move on (the effect
is more striking when this pop up is projected onto an entire wall!).


Finally, like PyEdit, PyClock can be run either standalone or attached to and embedded
in other GUIs that need to display the current time. When standalone, the windows
module from the preceding chapter (Example 10-16) is reused here to get a window
icon, title, and quit pop up for free. To make it easy to start preconfigured clocks, a
utility module called clockStyles provides a set of clock configuration objects you can
import, subclass to extend, and pass to the clock constructor; Figure 11-24 shows a
few of the preconfigured clock styles and sizes in action, ticking away in sync.


Run clockstyles.py (or select PyClock from PyDemos, which does the same) to recreate
this timely scene on your computer. Each of these clocks uses after events to check for
system-time rollover 10 times per second. When run as top-level windows in the same
process, all receive a timer event from the same event loop. When started as independ-
ent programs, each has an event loop of its own. Either way, their second hands sweep
in unison each second.


PyClock Source Code


All of the PyClock source code lives in one file, except for the precoded configuration
style objects. If you study the code at the bottom of the file shown in Example 11-12,
you’ll notice that you can either make a clock object with a configuration object passed
in or specify configuration options by command-line arguments such as the following
(in which case, the script simply builds a configuration object for you):


C:\...\PP4E\Gui\Clock> clock.py -bg gold -sh brown -size 300

Figure 11-23. PyClock countdown timer expired


754 | Chapter 11: Complete GUI Programs

Free download pdf