Python TKinter Gui:Time Tools, Threads, and Animation
The last stop on our widget tour is 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 example, 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 scheduling such delayed actions and forcing screen updates:
widget.after( milliseconds, function, *args)
This tool schedules the function to be called after a number of milliseconds. function can be any callable Python object: a function, bound method, and so on. This form of the call does not pause the programthe callback function is run later from the normal Tkinter event loop. The milliseconds value can be a floating-point number, to specify fractions of a second. This 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 program for a number of millisecondsfor example, an argument of 5,000 pauses for 5 seconds. This is essentially the same as Python’s library function, time.sleep, and both calls can be used to 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 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.
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, and see PyDraw in Chapter 12).
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 (technically, 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 argumentsthe 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 under Tk 8.0and is still not available on Windows XP under Python 2.4 and Tk 8.4 as I write this third editionit won’t be used in this book. Because it is 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 neededsee Chapter 11 for more details. Threads are a much more general solution to nonblocking data transfers.
widget.wait_variable(var)
widget.wait_window(win)
widget.wait_visibility(win)
These tools pause the caller until a Tkinter variable changes its value, a window is destroyed, or a window becomes visible. All of these enter a local event loop, such that the application’s mainloop continues to handle events. Note that var is a Tkinter variable object (discussed earlier), not a simple Python variable. To use for modal dialogs, first call widget.focus( ) (to set input focus) and widget.grab( ) (to make a window be the only one active).
We won’t go into further details on all of these tools here; see other Tk and Tkinter documentation for more information
没有评论▼