Hi Moray,

On Fri, 18 Sep 2009 06:00:16 -0400
"Moray Grieve" <m...@progress.com> wrote:

> The code fragment below has an example where a Tk GUI is launched as a
> separate thread - this all works fine in Python 2.5, but in Python 2.6
> the thread hangs. The issue seems to be in the line;
> 
>  
> 
> self.messageBoxDetails.insert(INSERT, "Hello world")
> 
>  
> 
> Comment this line out, or use an empty string rather than "Hello
> World", and the example works in both Python 2.5 and Python 2.6. I
> have not been able to see any related issues to this and wonder if
> this is a bug in the 2.6 Tkinter module?
> 
>  

Here (python-2.5.2 on debian linux) I get the following error message:

Unhandled exception in thread started by 
Traceback (most recent call last):
  File "test.py", line 49, in start
    self.parentContainer.mainloop()
  File "/usr/lib/python2.5/lib-tk/Tkinter.py", line 1026, in mainloop
    self.tk.mainloop(n)
RuntimeError: Calling Tcl from different appartment

I never saw this before, but it seems to nicely point on to what is
wrong with your code. You are trying to communicate with Tk from two
different threads which is generally a bad idea. Sometimes this may be
a source of confusion because the same code seems to work on one system
an crashes on another, the problem is that you never seem to know in
advance. The canonical way of using threads with Tkinter is to make
sure that all calls to the Tk interpreter are done from the main
program thread; if you need communication between a child thread and
Tk, it should be done for example by changing some variable's value
from within the child thread and periodically polling these variable's
value from the Tk thread.

I hope this helps

Michael

> 
> Thanks for any assistance.
> 
>  
> 
> -Moray
> 
>  
> 
>  
> 
> ---------------------
> 
> import time, thread
> 
> from Tkinter import *
> 
>  
> 
> class ExampleError:
> 
>      def __init__(self):
> 
>            self.parentContainer = Tk()
> 
>            self.parentContainer.protocol('WM_DELETE_WINDOW',
> self.quitPressed)
> 
>            self.parentContainer.wm_geometry("500x400")
> 
>            self.parentContainer.title("Parent Container Title")
> 
>            
> 
>            self.container = Frame(self.parentContainer)
> 
>            self.messageBoxDetails = Text(self.container, wrap=WORD,
> width=1, height=1, padx=10, pady=10)
> 
>            
> 
>            # in python 2.6, this next statement hangs when running as
>            # a
> thread - note that 
> 
>            # inserting an empty string works though
> 
>            self.messageBoxDetails.insert(INSERT, "d")
> 
>            self.messageBoxDetails.pack(fill=BOTH, expand=YES,
> side=LEFT)
> 
>            
> 
>            self.container.pack(fill=BOTH, expand=YES, padx=5, pady=5)
> 
>            
> 
>      def quitPressed(self):
> 
>            self.stop()
> 
>            
> 
>      def start(self):
> 
>            self.parentContainer.mainloop()
> 
>                 
> 
>      def stop(self):
> 
>            self.parentContainer.quit()
> 
>            self.parentContainer.destroy()
> 
>            
> 
>            
> 
>            
> 
> if __name__ =="__main__":
> 
>      example = ExampleError()
> 
>      time.sleep(1)
> 
>      thread.start_new_thread(example.start, ())
> 
>      time.sleep(5)   <http://www.progress.com/> 
> 
>  
> 
> 
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to