471,309 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Multithreaded tkinter GUI application exception handling

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.1080883 340"
---------

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

Jul 18 '05 #1
2 4102
Hello Michael,

Michael Zhang <ji*****@bigpond.com> wrote in message news:<BF*******************@news-server.bigpond.net.au>...
... 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: ...
Could someone help me out about those thread stuff? Thanks!


I also had the same exception, but I can't remember what was the
reason. You can look at my working code, maybe it can help:

http://uucode.com/texts/pylongopgui/pyguiapp.html
A complete Python Tkinter application demonstrates one of the ways to
implement a good GUI wrapper for a long operation: with a progress
bar, log messages, cancelling.

--
Oleg
Jul 18 '05 #2
Michael Zhang wrote:
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"
---------
Not knowing your application exactly, I can only guess what's happening: it
seems that your secondary thread is not aware that the window is closed and it
still tries to draw things in a canvas that has already been destroyed (module
realtime.py, line 238, according to the traceback).

So you should either make sure your secondary thread is stopped before the
window is closed, or enclose the canvas.create_image in a try except handling
the TclError. This last solution is far from optimal, since any mistake in a
Tkinter call always raises a TclError, so you may catch other errors as well.
I'd go for the first, using for example an Event (in module threading) to
indicate the window is closed.
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.1080883 340"
---------

Why is the "TclError"? How can I handle those exceptions and exit the
applicaton gracefully?
Same problem here, this time for the progress bar apparently: you try to do
things in a widget that is already destroyed.

BTW, mixing Tkinter calls in several threads is not a good idea: Tkinter is not
thread-safe and I experienced quite a lot of crashes when trying to do that. The
best solution I could find was to handle all the GUI stuff in the main thread
and make the secondary threads post events in Tkinter's event queue via the
event_generate method to serialize things at GUI level. I'm quite surprised to
hear that you succeeded in making things work, but be aware that you may
experience weird crashes in the future...
Could someone help me out about those thread stuff? Thanks!

Michael


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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Daniel Wilson | last post: by
1 post views Thread by Noor | last post: by
13 posts views Thread by tolisss | last post: by
4 posts views Thread by MadSage | last post: by
16 posts views Thread by Chuck Cobb | last post: by
5 posts views Thread by Bry | last post: by
1 post views Thread by Tom Berger | last post: by
reply views Thread by rosydwin | last post: by

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.