Tkinter - Reading widgets during a callback

Jeff Epler jepler at unpythonic.net
Mon Dec 9 16:35:59 EST 2002


In Tk, you can call 'update()' (a widget method) periodically to handle
pending user interface events.  Then the command of your "Cancel" button
can fire, setting some value that you can test for in the computation
loop.  Something like:

    def process(self):
        while not self.done and not self.abort:
            self.do_50ms_work() #*
            self.window.update()

* Quoting from GUI Bloopers by Jeff Johnson (p.383):
    7.3.3 The user interface is a real-time interface

    Like software that collects data from scientific or medical
    instruments, software that interacts with people should be designed
    to meet real-time constraints.  Three time constants in human
    behavior set goals that computer systems just meet in order to be
    perceived as responsivle [Nielsen, 1993; Robertson et al., 1989,
    1993]

    * The first time constant, 0.1 second, is the limit for perception
      of cause-and-effect between events.  If software waits longer than
      0.1 second to register a reaction to a user action, perception of
      cause-and-effect is "broken": the user will not associate the
      displayed reaction with his or her action.  This 0.1 second time
      constant is what Xerox PARC researcher Stuart Card and his
      collegues call the perceptual "moment".  It is also the
      approximate limit for perception of smooth animation

If you do about 50ms work between calls to update(), that means that you
give the machinery of Tk and Tkinter 50ms to respond to a button-press
and still come in under the .1s limit.

This means tuning your process loop over a wide range of processors and
system load conditions to continue to call update() this frequently.

This is one reason that many people advocate using threads in GUI
work.  You can keep good responsiveness in the UI thread, write your
computational thread in the straightforward way (with occasional checks
that the computation isn't canceled -- say, at least once a second--That's
the second time constant, the maximum length of time for which you need
not show a progress indicator), and everybody wins.

Jeff




More information about the Python-list mailing list