Tcl/Tk - updating your display while tasks are running
Archive - Originally posted on "The Horse's Mouth" - 2007-12-16 08:53:33 - Graham EllisLet me make two statements:
1. When you are popping up a new window from your program, the very last thing you want to see happen is for the window to gradually appear, with bits of it being resized as it comes on the screen - not only is such an operation irritating on the eye, but also it's burning up a lot of processor time in working and re-working the display which slows the whole operation down.
2. When you press on a button within a window, you're looking for near-instant feedback - gratification that the button press has indeed been accepted, and if your press starts a longer operation running on the computer, you're also hoping - I'm sure - to receive feedback while the job is happening. Maybe a progress bar, a moving graph, or something like that.
These two requirements are opposites.
In Tcl/Tk, the screen is automatically updated just before the Tcl/Tk application goes into an event wait loop - in other words, the windows will pop up / be updated just prior to the point at which user input is once again accepted. This is very efficient in terms of display (as you would expect from a system that was written years ago, when computers were much less powerful) and usually pleasing on the eye.
On that small proportion of occasions that you want to have your display dynamically updated, you can add an update idletasks command into your Tcl/Tk code at appropriate times. This command causes all queued screen updates to be performed, even if the user is not about to be prompted for further input straight thereafter.
Here's an example of what I mean.
Initial display:

When I click on the "check status" button it runs a ping test three times, at intervals of one second, to test out the connection. If and only if I use update idletasks, the following will be displayed while this ping test is going on:

and when the task has completed, you'll get a full report (in all cases, as the screen is automatically updated prior to being available for further user inputs)

Here's a snippet of code - the proc that's run when the "check status" button is pressed. Full code on our web site here
proc pingit {} {
global currenthost
.result config -text "running test ... please wait"
update idletasks
# If you comment the previous line out
# the user is left wondering what is going on!
catch {exec ping -c 3 $currenthost} result
# Note the use of catch to recover exec results
.result config -text $result
}
You may have guessed that I just completed a public Tk course - inspiration to write up in more details and for "Joe Public" many of the issues that we cover on the course. GUI design - topics like fill and expand, sticky news, canvas scaling and update idletasks are perfect to teach and learn in the classroom, but are very hard to pick up from the books. Our Tk course is always a very effective one and students leave having really gained a huge amount that would be so hard to appreciate fully through self-study!