a_random_programmer
a_random_programmer

Reputation: 148

Trouble executing code with the tkinter.Tk() class

I'm trying to execute Tcl from python using the tkinter.Tk() class. My problem is that every time I try to run my code, I get the error shown at the bottom of the question

FYI: my Tcl code is saved to the file "calc.tcl" and my python file is called 'go.py'

my python code is:

from tkinter import *
root=Tk()
code=open('calc.tcl').read()
root.tk.call('exec'code)
root.mainloop()

my Tcl code is:

frame .fr
pack .fr

proc calc {} {
    set text [.fr.ent1 get]
    if {[catch {set result [expr $text]}]} {
        set result "calculation failed"
    }
    .fr.ent2 delete 0 end 
    .fr.ent2 insert 1 $result
} 

entry .fr.ent1
bind .fr.ent1 <Return> {calc}
entry .fr.ent2

grid .fr.ent1 -row 0 -column 0
grid .fr.ent2 -row 1 -column 0

the error is:

Traceback (most recent call last):
  File "go.py", line 4, in <module>
    root.tk.call('exec',code)
_tkinter.TclError: couldn't execute "

frame .fr
pack .fr

proc calc {} {
    set text [.fr.ent1 get]
    if {[catch {set result [expr $text]}]} {
        set result "calculation failed"
    }
    .fr.ent2": file name too long

Upvotes: 0

Views: 41

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137567

In Tcl, exec runs a subprocess defined by an executable file with the given name (which, as it has spaces and newlines and so on in it, is really unusual for a filename, giving you an error message because you've not got such an executable). You probably want to use eval instead.

root.tk.call('eval', code)

Upvotes: 2

Related Questions