Multithreaded tkinter GUI application exception handling

From: Michael Zhang (jianqiz_at_bigpond.com)
Date: 10/12/04


Date: Tue, 12 Oct 2004 14:14:57 GMT

My project uses Python-2.3.4 + Tkinter + PIL-1.1.4 to retrieve images
from server and display those images.

I created a thread (also a separate toplevel window) for displaying
images and another thread for recording the frame rates (using a
progress bar for visulization). The whole application worked very well
once it received image data from the socket.

The problem is when I tried to close that display window (click on the
standard "X" button on the right-upper corner), I got this exception:

--------
Exception in thread Thread-1:
Traceback (most recent call last):
   File "/usr/local/lib/python2.3/threading.py", line 436, in __bootstrap
     self.run()
   File "realtime.py", line 188, in run
     UI(self.master)
   File "realtime.py", line 238, in __init__
     self.canvas.create_image(0, 0, image=self.image, anchor=NW)
   File "/usr/local/lib/python2.3/lib-tk/Tkinter.py", line 2060, in
create_image
     return self._create('image', args, kw)
   File "/usr/local/lib/python2.3/lib-tk/Tkinter.py", line 2049, in _create
     return getint(self.tk.call(
TclError: invalid command name ".1080883980.1080884908"
---------

Then after I closed the main windows, I got another exception:

---------
Exception in thread Thread-2:
Traceback (most recent call last):
   File "/usr/local/lib/python2.3/threading.py", line 436, in __bootstrap
     self.run()
   File "realtime.py", line 198, in run
     self.bar.updateProgress(i)
   File "/home/michael/usyd/soft3700/py/realtime/progressbar.py", line
73, in updateProgress
     self.update()
   File "/home/michael/usyd/soft3700/py/realtime/progressbar.py", line
91, in update
     float(value) / self.max * self.width, self.height)
   File "/usr/local/lib/python2.3/lib-tk/Tkinter.py", line 2039, in coords
     self.tk.splitlist(
TclError: invalid command name
".1080881420.connectionPane.1080883148.1080883340"
---------

Why is the "TclError"? How can I handle those exceptions and exit the
applicaton gracefully?

Could someone help me out about those thread stuff? Thanks!

Michael