MovingPicsThreaded(root)
mainloop()
PyClock: An Analog/Digital Clock Widget
One of the first things I always look for when exploring a new computer interface is a
clock. Because I spend so much time glued to computers, it’s essentially impossible for
me to keep track of the time unless it is right there on the screen in front of me (and
even then, it’s iffy). The next program, PyClock, implements such a clock widget in
Python. It’s not substantially different from the clock programs that you may be used
to seeing on the X Window System. Because it is coded in Python, though, this one is
both easily customized and fully portable among Windows, the X Window System,
and Macs, like all the code in this chapter. In addition to advanced GUI techniques,
this example demonstrates Python math and time module tools.
A Quick Geometry Lesson
Before I show you PyClock, though, let me provide a little background and a confession.
Quick—how do you plot points on a circle? This, along with time formats and events,
turns out to be a core concept in clock widget programs. To draw an analog clock face
on a canvas widget, you essentially need to be able to sketch a circle—the clock face
itself is composed of points on a circle, and the second, minute, and hour hands of the
clock are really just lines from a circle’s center out to a point on the circle. Digital clocks
are simpler to draw, but not much to look at.
Now the confession: when I started writing PyClock, I couldn’t answer the last para-
graph’s opening question. I had utterly forgotten the math needed to sketch out points
on a circle (as had most of the professional software developers I queried about this
magic formula). It happens. After going unused for a few decades, such knowledge
tends to be garbage collected. I finally was able to dust off a few neurons long enough
to code the plotting math needed, but it wasn’t my finest intellectual hour.‡
If you are in the same boat, I don’t have space to teach geometry in depth here, but I
can show you one way to code the point-plotting formulas in Python in simple terms.
Before tackling the more complex task of implementing a clock, I wrote the plotter
Gui script shown in Example 11-11 to focus on just the circle-plotting logic.
Its point function is where the circle logic lives—it plots the (X,Y) coordinates of a point
on the circle, given the relative point number, the total number of points to be placed
on the circle, and the circle’s radius (the distance from the circle’s center to the points
drawn upon it). It first calculates the point’s angle from the top by dividing 360 by the
‡ Lest that make software engineers seem too doltish, I should also note that I have been called on repeatedly
to teach Python programming to physicists, all of whom had mathematical training well in advance of my
own, and many of whom were still happily abusing FORTRAN common blocks and go-tos. Specialization
in modern society can make novices of us all.
PyClock: An Analog/Digital Clock Widget | 747