GUI (Tkinter) and "cancel"

Randall Hopper aa8vb at yahoo.com
Fri May 19 08:20:07 EDT 2000


 |I have a simple graphical user interface written with Tkinter. This
 |allows a user to call a function by pressing on a button (or something
 |like this). The function works for some minutes, somtimes up to an hour.
 |How can I implement a "cancel"-button that allows the user to cancel the
 |time-intensive function.
 |
 |At first I thought on creating another thread, but it must be simpler.
 |How?

     The essence of the problem is you need to have your GUI (Tkinter)
process periodically checking for user events, either by returning to the
main event loop or calling one of the "check for user events" APIs
(Tkinter's update method for example--though see caveats in the docs).

You have a number of options:

   1) Modify your work function to periodically check for user events.

                       worker() -> check_events()
                                -> check_events()
                                -> check_events()

   2) Change your work function to be event-driven.  That is, to do its
      work in pieces, saving appropriate state betweeen each piece.  Just
      wake up to do a slice of work via Tkinter timers, and return to the
      main event loop after each slice to handle user events.

                       tk_timer_event -> worker()
                       tk_timer_event -> worker()
                       tk_timer_event -> worker()

   3) Fork off a subprocess to do the work.
   
   4) Create a thread to do the work.

Which one is simplest really depends on your situation (how much coupling
there is between the GUI "thread" and the compute "thread", for example,
using the term thread loosely here).

-- 
Randall Hopper
aa8vb at yahoo.com




More information about the Python-list mailing list