Catching exceptions in Tkinter's mainloop

Richard Townsend NOstarfighterSPAM at freeuk.com
Tue Mar 20 14:01:52 EST 2001


"Mike Callahan" <mcalla at home.com> wrote in message
news:owzt6.21061$Q47.5935326 at news1.rdc1.tn.home.com...
> I tried the following with no luck:
>

> import Tkinter as T
> import sys
>
> class Gui:
>     def __init__(self, master):
>         T.Button(master, text='Crash2', command=self.crash).pack()
>
>     def crash(self):
>         z = zz
>
> class Catcher:
>     def __init__(self, func, subst, widget):
>         self.func = func
>         self.subst = subst
>         self.widget = widget
>
>     def __call__(self, *args):
>         try:
>             if self.subst:
>                 args = apply(self.subst, args)
>             return apply(self.func, args)
>         except SystemExit, msg:
>             raise SystemExit, msg
>         except:
>             import traceback
>             traceback.print_exc()
>             self.widget.quit()
>
> root = T.Tk()
> f = open('test.log', 'w')
> sys.stderr = f
> T.CallWrapper = Catcher
> app = Gui(root)
> root.mainloop()
>
> What am I doing wrong?
>

I've just tried your code and it wrote the following to the 'test.log' file:

Traceback (most recent call last):
  File "catcher.py", line 21, in __call__
    return apply(self.func, args)
  File "catcher.py", line 9, in crash
    z = zz
NameError: There is no variable named 'zz'


Is that not what you wanted ??

Richard






More information about the Python-list mailing list