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

(yzsuai) #1

upon other cells. In the interest of space, further mutations toward that goal are left as
exercises.


I should also point out that there is more to gridding than we have time to present fully
here. For instance, by creating subframes that have grids of their own, we can build up
more sophisticated layouts as component hierarchies in much the same way as nested
frames arranged with the packer. For now, let’s move on to one last widget survey topic.


Time Tools, Threads, and Animation


The last stop on our widget tour is perhaps the most unique. tkinter also comes with
a handful of tools that have to do with the event-driven programming model, not
graphics displayed on a computer screen.


Some GUI applications need to perform background activities periodically. For exam-
ple, to “blink” a widget’s appearance, we’d like to register a callback handler to be
invoked at regular time intervals. Similarly, it’s not a good idea to let a long-running
file operation block other activity in a GUI; if the event loop could be forced to update
periodically, the GUI could remain responsive. tkinter comes with tools for both sched-
uling such delayed actions and forcing screen updates:


widget.after(milliseconds, function, *args)
This tool schedules the function to be called once by the GUI’s event processing
system after a number of milliseconds. This form of the call does not pause the
program—the callback function is scheduled to be run later from the normal
tkinter event loop, but the calling program continues normally, and the GUI re-
mains active while the function call is pending. As also discussed in Chapter 5,
unlike the threading module’s Timer object, widget.after events are dispatched in
the main GUI thread and so can freely update the GUI.
The function event handler argument can be any callable Python object: a function,
bound method, lambda and so on. The milliseconds timer duration argument is
an integer which can be used to specify both fractions and multiples of a second;
its value divided by 1,000 gives equivalent seconds. Any args arguments are passed
by position to function when it is later called.
In practice, a lambda can be used in place of individually-listed arguments to make
the association of arguments to function explicit, but that is not required. When
the function is a method, object state information (attributes) might also provide
its data instead of listed arguments. The after method returns an ID that can be
passed to after_cancel to cancel the callback. Since this method is so commonly
used, I’ll say more about it by example in a moment.


widget.after(milliseconds)
This tool pauses the calling program for a number of milliseconds—for example,
an argument of 5,000 pauses the caller for 5 seconds. This is essentially equivalent
to Python’s library function time.sleep(seconds), and both calls can be used to


582 | Chapter 9: A tkinter Tour, Part 2

Free download pdf