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

(yzsuai) #1
add a delay in time-sensitive displays (e.g., animation programs such as PyDraw
and the simpler examples ahead).

widget.after_idle(function, *args)
This tool schedules the function to be called once when there are no more pending
events to process. That is, function becomes an idle handler, which is invoked
when the GUI isn’t busy doing anything else.


widget.after_cancel(id)
This tool cancels a pending after callback event before it occurs; id is the return
value of an after event scheduling call.


widget.update()
This tool forces tkinter to process all pending events in the event queue, including
geometry resizing and widget updates and redraws. You can call this periodically
from a long-running callback handler to refresh the screen and perform any updates
to it that your handler has already requested. If you don’t, your updates may not
appear on-screen until your callback handler exits. In fact, your display may hang
completely during long-running handlers if not manually updated (and handlers
are not run in threads, as described in the next section); the window won’t even
redraw itself until the handler returns if covered and uncovered by another.
For instance, programs that animate by repeatedly moving an object and pausing
must call for an update before the end of the animation or only the final object
position will appear on-screen; worse, the GUI will be completely inactive until
the animation callback returns (see the simple animation examples later in this
chapter, as well as PyDraw in Chapter 11).


widget.update_idletasks()
This tool processes any pending idle events. This may sometimes be safer than
after, which has the potential to set up race (looping) conditions in some scenarios.
Tk widgets use idle events to display themselves.


_tkinter.createfilehandler(file, mask, function)
This tool schedules the function to be called when a file’s status changes. The
function may be invoked when the file has data for reading, is available for writing,
or triggers an exception. The file argument is a Python file or socket object (tech-
nically, anything with a fileno() method) or an integer file descriptor; mask is
tkinter.READABLE or tkinter.WRITABLE to specify the mode; and the callback
function takes two arguments—the file ready to converse and a mask. File handlers
are often used to process pipes or sockets, since normal input/output requests can
block the caller.
Because this call is not available on Windows, it won’t be used in this book. Since
it’s currently a Unix-only alternative, portable GUIs may be better off using
after timer loops to poll for data and spawning threads to read data and place it
on queues if needed—see Chapter 10 for more details. Threads are a much more
general solution to nonblocking data transfers.


Time Tools, Threads, and Animation | 583
Free download pdf