473,241 Members | 1,520 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,241 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 4324
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
1
by: Noor | last post by:
Hi all, I am trying to catch all types of exceptions from a app regardless of whether it is in debugger mode( VS development environment) or run the.exe file outside the IDE. My App...
13
by: tolisss | last post by:
Hi i have setup a global exception handler b4 Application.Run like Application.ThreadException += new ThreadExceptionEventHandler(GlobalExceptionProcessing.AppThreadException ); then after...
11
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
4
by: MadSage | last post by:
I currently have a multi-threaded server application with worker threads and a core thread. For all of these threads I have something like the following code: uint32 __stdcall...
16
by: Chuck Cobb | last post by:
I'm implementing a centralized exception handling routine using the Enterprise Library Exception Management Application Block. I trap all unhandled exceptions to one place using the following...
5
by: Bry | last post by:
I've created a class that offers an enhanced way of handling fatal exceptions. The class allows the user to optionaly submit a http based anonymous error report to myself, and also records details...
1
by: Tom Berger | last post by:
Just a short question concerning exception handling.... All of my applications contain an event handling in the Main() like this one: static void Main() { // declare global exeption...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.