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

(yzsuai) #1

and digital displays are both constructed when the script starts, but only one is ever
packed at any given time.


A right mouse click on the clock in either display mode shows or hides an attached
label that gives the current date in simple text form. Figure 11-22 shows a PyClock
running with an analog display, a clicked-on date label, and a centered photo image
object (this is clock style started by the PyGadgets launcher):


Figure 11-22. PyClock extended display with an image


The image in the middle of Figure 11-22 is added by passing in a configuration object
with appropriate settings to the PyClock object constructor. In fact, almost everything
about this display can be customized with attributes in PyClock configuration
objects—hand colors, clock tick colors, center photos, and initial size.


Because PyClock’s analog display is based upon a manually sketched figure on a canvas,
it has to process window resize events itself: whenever the window shrinks or expands,
the clock face has to be redrawn and scaled for the new window size. To catch screen
resizes, the script registers for the event with bind; surprisingly, this isn’t
a top-level window manager event like the Close button. As you expand a PyClock, the
clock face gets bigger with the window—try expanding, shrinking, and maximizing the
clock window on your computer. Because the clock face is plotted in a square coordi-
nate system, PyClock always expands in equal horizontal and vertical proportions,
though; if you simply make the window only wider or taller, the clock is unchanged.


Added in the third edition of this book is a countdown timer feature: press the “s” or
“m” key to pop up a simple dialog for entering the number of seconds or minutes for
the countdown, respectively. Once the countdown expires, the pop up in
Figure 11-23 appears and fills the entire screen on Windows. I sometimes use this in


PyClock: An Analog/Digital Clock Widget | 753
Free download pdf