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

(yzsuai) #1

Other animation effects


Besides canvas-based animations, widget configuration tools support a variety of ani-
mation effects. For example, as we saw in the flashing and hiding alarm scripts earlier
(see Example 9-28), it is also easy to change the appearance of other kinds of widgets
dynamically with after timer-event loops. With timer-based loops, you can periodi-
cally flash widgets, completely erase and redraw widgets and windows on the fly, re-
verse or change widget colors, and so on. See “For a Good Time...” on page 51 for
another example in this category which changes fonts and colors on the fly (albeit, with
questionable ergonomic intentions).


Threads and animation


Techniques for running long-running tasks in parallel threads become more important
if animations must remain active while your program waits. For instance, imagine a
program that spends minutes downloading data from a network, calculating the output
of a numeric model, or performing other long-running tasks. If such a program’s GUI
must display an animation or otherwise show progress while waiting for the task, it can
do so by either altering a widget’s appearance or by moving objects in a canvas peri-
odically—simply use the after method to wake up intermittently to modify the GUI
as we’ve seen. A progress bar or counter, for instance, may be updated during after
timer-event handling.


In addition, though, the long-running task itself will likely have to be run in a spawned
parallel thread so that your GUI remains active and performs the animation during the
wait. Otherwise, no GUI updates will occur until the task returns control to the GUI.
During after timer-event processing, the main GUI thread might check variables or
objects set by the long-running task’s thread to determine completion or progress.


Especially if more than one long-running task may be active at the same time, the
spawned thread might also communicate with the GUI thread by storing information
in a Python Queue object, to be picked up and handled by the GUI during after events.
For generality, the Queue might even contain function objects that are run by the GUI
to update the display.


Again, we’ll study such threaded GUI programming and communication techniques
in Chapter 10, and employ them in the PyMailGUI example later in the book. For now,
keep in mind that spawning computational tasks in threads can allow the GUI itself to
both perform animations and remain active in general during wait states.


Graphics and gaming toolkits


Unless you stopped playing video games shortly after the ascent of Pong, you probably
also know that the sorts of movement and animation techniques shown in this chapter
and book are suitable for some simple game-like programs, but not all. For more de-
manding tasks, Python also has additional graphics and gaming support we haven’t
studied here.


594 | Chapter 9: A tkinter Tour, Part 2

Free download pdf