thread and tkMessageBox

Eric Brunel eric_brunel at despammed.com
Fri Sep 17 11:26:29 EDT 2004


Ajay wrote:
> hi!
> 
> my application consists of a GUI with a number of functions. One of these
> runs a server in a separate thread. the thread is started and given the
> function start_server to execute
> the problem is when within start_server i try to display anything using
> tkMessageBox, nothing gets displayed and my application crashes.

Don't do that: Tkinter is not thread-safe; almost every call to Tkinter methods 
or functions should be done in the thread where the mainloop was started, or you 
will experience such crashes.

> Any ideas
> what i am doing wrong and how to correct it

The usual way to do that is to post an event to the GUI event queue via the 
event_generate method and to treat this event in the mainloop thread. Since you 
seem to display a (supposedly modal) dialog in the secondary thread, this will 
involve a synchronisation mechanism between the two threads. Here is an example:

-----------------------------------------------------------
import threading, time
from Tkinter import *
from tkMessageBox import askyesno
from Queue import Queue

## Tkinter calls are in the main thread
root = Tk()

## Communication queue betwwen the two threads
q = Queue()

## Function for secondary thread
def th2():
   ## Simulate a long treatment
   time.sleep(1)
   ## Generate event to display dialog
   root.event_generate('<<Ask>>', when='tail')
   ## Wait for answer coming from main thread
   answer = q.get()
   ## Continue after answer was received
   print answer
   time.sleep(1)
   print 'Done'

## Binding for <<Ask>> custom event
def ask(event=None):
   ## Display dialog
   answer = askyesno("OK?", "OK to go?")
   ## Put event in queue
   q.put(answer)

root.bind('<<Ask>>', ask)

## Run secondary thread
th = threading.Thread(target=th2)
th.start()

## Run mainloop
root.mainloop()
-----------------------------------------------------------

As you can see, the secondary thread requests the display of the dialog via a 
custom event named <<Ask>>. This event is treated by a regular binding in the 
main thread (this is an exception to the rule above: calling the Tkinter method 
event_generate in a secondary thread works)

To communicate the dialog result between threads, a regular Queue is used: once 
the event has been put in the event queue, the secondary thread tries to read 
the answer from the queue in blocking mode. The answer got from the dialog in 
the main thread is put in the queue, and received by the secondary thread.

All this is a bit complicated, but it's generally needed when you work in a GUI 
application with several threads, as GUI toolkits are generally not thread-safe.

HTH
-- 
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com




More information about the Python-list mailing list