www.gibmonks.com

  Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 36.  Focus, Grabs, and Dialogs


Animation with the update Command

Suppose you want to entertain your user while your application is busy. By default, the user interface hangs until your processing completes. Even if you change a label or entry widget in the middle of processing, the updates to that widget are deferred until an idle moment. The user does not see your feedback, and the window is not refreshed if it gets obscured and uncovered. The solution is to use the update command that forces Tk to go through its event loop and update the display.

The next example shows a Feedback procedure that displays status messages. A read-only entry widget displays the messages, and the update command ensures that the user sees each new message. An entry widget is used because it won't change size based on the message length, and it can be scrolled by dragging with the middle mouse button. Entry widgets also work better with update idletasks as described later:

Example 36-3 A feedback procedure.
proc Feedback { message } {
   global feedback
   set e $feedback(entry)
   $e config -state normal
   $e delete 0 end
   $e insert 0 $message
   # Leave the entry in a read-only state
   $e config -state disabled
   # Force a display update
   update idletasks
}

The Tk widgets update their display at idle moments, which basically means after everything else is taken care of. This lets them collapse updates into one interaction with the window system. On UNIX, this improves the batching effects that are part of the X protocol. A call to update idletasks causes any pending display updates to be processed. Chapter 16 describes the Tk event loop in more detail.

graphics/tip_icon.gif

Use update idletasks if possible.


The safest way to use update is with its idletasks option. If you use the update command with no options, then all events are processed. In particular, user input events are processed. If you are not careful, it can have unexpected effects because another thread of execution is launched into your Tcl interpreter. The current thread is suspended and any callbacks that result from input events are executed. It is usually better to use the tkwait command if you need to process input because it pauses the main application at a well-defined point.

One drawback of update idletasks is that in some cases a widget's redisplay is triggered by window system events. In particular, when you change the text of a label, it can cause the size of the label to change. The widget is too clever for us in this case. Instead of scheduling a redisplay at idle time, it requests a different size and then waits for the <Configure> event from the window system. The <Configure> event indicates a size has been chosen by the geometry manager, and it is at that point that the label schedules its redisplay. So, changing the label's text and doing update idletasks do not work as expected.


      Previous section   Next section
    Top