Catching exceptions in Tkinter's mainloop

Mike Callahan mcalla at home.com
Thu Mar 22 03:36:32 CET 2001


Richard,

On my system, the exception is captured and printed in test.log but mainloop
is not exited. Is this what happens on your system?

Mike C.

"Richard Townsend" <NOstarfighterSPAM at freeuk.com> wrote in message
news:6QNt6.37618$g63.4509118 at nnrp3.clara.net...
> "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