[Tutor] help with Tkinter, please

wesley chun wescpy at gmail.com
Tue Nov 28 08:32:55 CET 2006


> >>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.
> >
> >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.
>
> Thanks, John. I know nothing about threading. I see that Chun's _Core
> Python Programming_, 2nd ed. has a 30-page chapter on it. A good
> place to start?


it is a good place to start, but there isn't a combination
GUI+threading example in the book... perhaps 3rd edition....

in order to make your app "do what you want," you need to have the
main thread running the GUI, e.g., mainloop(), and fire off threads to
do the calculations.  if your operating system supports daemon
threads, set your unimportant threads as daemon threads -- from the
docs:

"The entire Python program exits when no active non-daemon threads are left."

http://docs.python.org/lib/thread-objects.html

if your OS threads don't support this, then you have to manage a
shared "QUIT" variable which you can set in your quit routine which
calls root.quit() if root is your main Tk window (sys.exit() is a
little rough), and have your worker threads continually check the
status of such a flag during execution. when the user quits, this flag
can be set, telling the workers to put down their shovels and go home.
 :-)

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list