[Tutor] help with Tkinter, please

John Fouhy john at fouhy.net
Mon Nov 27 05:55:26 CET 2006

On 27/11/06, Dick Moores <rdm at rcblue.com> wrote:
> I have a question about the "EXIT" button's command,
> command=sys.exit. It fails to quit the program when I've entered too
> large an integer for factoring, or for prime testing, and I don't
> want to wait any more for the result. When I'm running the program
> from my Win XP console, I can quit with a simple Ctrl+C, but this
> won't be available to the friend I may send the executable to. Is it
> possible to build in a way to smoothly interrupt, say, a factoring
> process that's taking too long?

Well, as you may know, the "core" of Tkinter (or most GUIs, I think)
is the event loop.  The event loop basically does this:
 1. Look for events (mouse clicks, keypresses, etc.)
 2. Process events (update widgets, call callbacks)
 3. GOTO 1.

So, in this specific case, the event loop is doing this:

1. Look for events ==> user clicks on "factorise" button.
2. Process events ==> call "factorise" button callback.
3. Callback runs...
4. ...callback completes.
5. Look for events ==> user clicks on "exit" button.
6. Process events ==> call "exit" button callback.
7. Python exits.

So, you can see why clicking your exit button won't interrupt the
calculation in progress.  The only way you can do that is if you can
get the event loop to start looking for events again before the
callback finishes.  And that means THREADS.

HTH :-)


More information about the Tutor mailing list