[Tkinter-discuss] tkinter error handling when tcl calls python functions

Patrick Dunnigan patrick.dunnigan at cloudsidekick.com
Fri Aug 3 19:51:35 CEST 2012


Hi all, 

Scenario: 

I am transitioning a mature tcl package to python. As in interim step in the long process, I will use tkinter in a python script to call the tcl code. Since I have some common functionality already written in python, I am using the createcommand function to expose this to tcl. As a side note, there is no tk in this software, only using the tcl piece. Another side note: the useTk=0 argument appears to have no effect.   

Everything works nicely. However I have found that when the tcl code calls a python function and an error occurs in the python, the python error is not included in the tcl error stack. I've spent hours hacking away trying to figure it out, but now it's time to turn to the community. 

Here's a boiled down script that can be used to reproduce and illustrate:

example 1, this is the best message I can get out of tcl, but doesn't include the error that occurs in the "hello" function:

#!/usr/bin/env python
from Tkinter import Tcl

def hello():
    # the error will occur on the following line --->
    print z
    return
tcl = Tcl()
tcl.createcommand('hello', hello)

try:
    tcl.eval('hello')
except Exception as e:
    print tcl.getvar(name="errorInfo")
    raise e

./test.py

    while executing
"hello"
Traceback (most recent call last):
  File "./test.py", line 15, in <module>
    raise e
_tkinter.TclError


Example 2, this is what I would expect the tcl error message to include:

$ python
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print z
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'z' is not defined


The ideal error message would be something like (or with more traceback nesting if in a tcl procedure):

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'z' is not defined
    while executing
"hello"
Traceback (most recent call last):
  File "./test.py", line 15, in <module>
    raise e
_tkinter.TclError



Thanks,
Patrick



More information about the Tkinter-discuss mailing list