[Python-bugs-list] [ python-Bugs-639266 ] Tkinter sliently discards all Tcl errors

noreply@sourceforge.net noreply@sourceforge.net
Sun, 17 Nov 2002 07:39:38 -0800

Bugs item #639266, was opened at 2002-11-16 01:35
You can respond by visiting: 

Category: Tkinter
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Internet Discovery (idiscovery)
Assigned to: Nobody/Anonymous (nobody)
Summary: Tkinter sliently discards all Tcl errors

Initial Comment:

Tkinter silently discards all Tcl errors.  Athough this may make Tkinter programs
appear robust, it violates the fundamental principle that erros should be raised
and dealt with.

In Tkinter.py is the line

        self.tk.createcommand('tkerror', _tkerror)

where _tkerror is defined as

def _tkerror(err):
    """Internal function."""

This disables all uses of the tcl procedure bgerror from signalling
background errors to the user, including I assume any errors
generated in after_idle.


Comment By: Jeff Epler (jepler)
Date: 2002-11-17 09:39

Logged In: YES 

You could create a subclass of Tk that creates a different
'tkerror' command.

Anyway, CallWrapper (used when a function is passed to
functions like .after_idle()) calls
widget._report_exception, which calls
Tk.report_callback_exception.  So you can override this
function too.

Neither of these things require changing Tkinter.py.

I don't seem to be permitted to attach a file, so I'm forced
to paste the code.

import Tkinter as _Tkinter

class Tk(_Tkinter.Tk):
    def __init__(self, *args, **kw):
        _Tkinter.Tk.__init__(self, *args, **kw)
        self.tk.createcommand('tkerror', self.tkerror)

    def report_callback_exception(self, exc, val, tb):
        print "callback exception", exc

    def tkerror(self, *args):
        print "tkerror", args

if __name__ == '__main__':
    w = Tk()
    w.after_idle(lambda: 1/0)
    _Tkinter.Button(w, command="expr 1/0").pack()


Comment By: Martin v. L÷wis (loewis)
Date: 2002-11-16 03:33

Logged In: YES 

The qualification "silently discards all Tcl errors" is definitely 

>>> Tkinter.Label(foo="bar")
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in ?
  File "E:\Python22\lib\lib-tk\Tkinter.py", line 2261, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "E:\Python22\lib\lib-tk\Tkinter.py", line 1756, in __init__
TclError: unknown option "-foo"

Instead, it would be more correct to say that all background 
errors are discarded.

Can you provide a script that demonstrates this problem? 
(i.e. one which behaves differently if the createcommand is 
removed from Tkinter.py)

Can you propose a solution for this problem? Please take 
into account that Tkinter behaved that way since the 
beginning, so any change must take backwards compatibility 
into account.


You can respond by visiting: