thread and tkMessageBox

A

Ajay

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. Any ideas
what i am doing wrong and how to correct it

thanks
 
E

Eric Brunel

Ajay said:
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
 
Joined
Oct 9, 2008
Messages
1
Reaction score
0
Hi,

I followed the code of Eric and got it almost working.
The artificial event is received by the main thread but then the whole program crashes when it tries to call askopenfilename() in the main thread !?

Any idea what I could be doing wrong?
I'm using winXP with Python2.5.

Thanx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top