Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Exception handler mechanism for Tkinter?

334 views
Skip to first unread message

Zachary Roadhouse

unread,
Feb 1, 1997, 3:00:00 AM2/1/97
to

Would it be possible to change Tkinter to allow the user to define a
function which gets called instead of the current traceback printout?

Right now I'm using the following work around at the beginning of my
Tkinter script:

def report_callback_exception(self, exc, val, tb):
import tkTraceback
print "Exception in Tkinter callback"
tkTraceback.print_exception(exc, val, tb)

Tkinter.Tk.report_callback_exception = report_callback_exception

app = Tkinter.Tk()


This doesn't guarantee that every Tkinter exception will be handled by
my own handler. Maybe if a global variable tk_except_handler was
defined, Tkinter could use this instead. Such code might look like
this:

class Tk....

def report_callback_exception(self, exc, val, tb):
if tk_except_handler:
try:
tk_except_handler(exc,val,tb)
except:
pass
else:
import traceback
print "Exception in Tkinter callback"
traceback.print_exception(exc, val, tb)

or maybe:

def default_except_handler(exc,val,tb):
import traceback
print "Exception in Tkinter callback"
traceback.print_exception(exc, val, tb)

tk_except_handler = default_except_handler

class Tk....

def report_callback_exception(self, exc, val, tb):
try:
tk_except_handler(exc,val,tb)
except:
pass

What do you think? This would allow programming environments like
PTUI (and hopefully others yet to be created) to provide a better way
to trap callbacks.
--

- Zack

E-MAIL: Zachary_...@brown.edu WEB: http://althor.netspace.org/~zack/
Brown University, Box 220, Providence, RI 02912
Phone: (401) 863 - 5435

Fredrik Lundh

unread,
Feb 4, 1997, 3:00:00 AM2/4/97
to

> If an uncaught exception occurs, the programmer must be able to deal
> with gracefully - at least it should be displayed in a window so the
> user is aware there is a problem and preferably automatically email
> the error to the system administrator as a bug report.

Why not simply reroute stdout?

/F

Zachary_...@brown.edu

unread,
Feb 4, 1997, 3:00:00 AM2/4/97
to

Because my program may be imbedded in another. When code is running, I
rerouted stdout and stderr to text widgets which imitate files. During
other times, this may not be acceptable since other code may need the real
stdout and stderr. I'll give an example. PTUI was originally part of a
larger application called Raphael. Raphael is a large complex system for
running/testing image processing and computer vision code. Having a
python code editor inside the system proved usefull (didn't have to wait 1
min for Raphael to start up,) but Raphael does its own stuff to stdout and
stderr. Permanently switching stdout and stderr isn't an option.

Also, I want to be able to start up the debugger when an exception occurs.
I can trap exceptions in executed code, but Tkinter callbacks are much
harder to catch.

0 new messages