473,385 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

thread and tkMessageBox

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

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
Jul 18 '05 #1
1 5202
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

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Peter Kleiweg | last post by:
I have a program with these fragments: from Tkinter import * import tkFileDialog import tkMessageBox def openProject(): filepath = tkFileDialog.askopenfilename(filetypes=(("project...
1
by: Peter Kleiweg | last post by:
Here is a minimal example that produces the strange results: #### begin code #### #!/usr/bin/env python from Tkinter import * import tkFileDialog import tkMessageBox
2
by: Nathan | last post by:
Hi, I've been testing the standard dialog boxes in tkMessageBox under IDLE. If I type for example, tkMessageBox.askyesno('test', 'test'), the dialog box comes up fine but another window also...
4
by: jmdeschamps | last post by:
why is the button sunken when called through a bind method, and not with the command attribute? Thank you! ## Cut'nPaste example from Tkinter import * import tkMessageBox class...
6
by: marcoberi | last post by:
Hi everybody. I have this code snippet that shows a window without a titlebar (using overrideredirect) and two buttons on it: one quits and the other one brings up a simple tkMessageBox. On...
0
by: dudeja.rajat | last post by:
Hi, I'm working on Windows Platform I'm facing some problem with the tkMessageBox. My code is as below: import tkMessageBox import Tix from Tkinter import * if len(installedLibPath) !=...
0
by: Guilherme Polo | last post by:
On Thu, Aug 28, 2008 at 10:29 AM, <dudeja.rajat@gmail.comwrote: It is good to post a short code sample that demonstrates the problem, but it has to run by itself at least. tkMessageBox...
0
by: dudeja.rajat | last post by:
Hi, I'm using a TkMessageBox for handling some errors and displaying them through the message boxes. My code is as below: if selectedVer == strNoArchivedResults: tkMessageBox._show("Error",...
0
by: Guilherme Polo | last post by:
On Mon, Sep 1, 2008 at 3:35 PM, <dudeja.rajat@gmail.comwrote: Note that you are not supposed to use _show, use showerror here instead. Remove the call to sys.exit then ? Your ask for a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.