Using Python functions in Tkinter.Tcl()

有些话、适合烂在心里 提交于 2019-12-03 17:35:40

With a little experimentation I discovered you can do something like this to create a tcl interpreter, register a python command, and call it from Tcl:

import Tkinter

# create the tcl interpreter
tcl = Tkinter.Tcl()

# define a python function
def pycommand(*args):
    print "pycommand args:", ", ".join(args)

# register it as a tcl command:
tcl_command_name = "pycommand"
python_function = pycommand
cmd = tcl.createcommand(tcl_command_name, python_function)

# call it, and print the results:
result = tcl.eval("pycommand one two three")
print "tcl result:", result

When I run the above code I get:

$ python2.5 /tmp/example.py
pycommand args: one, two, three
tcl result: None

@Brian - I had to experiment in order to get the right result

from Tkinter import Tcl
tcl = Tcl()
result = tcl.eval(' puts "hello, world" ')

Note the placement of the single and double quotes. This gave me the expected output: hello, world

Any other combinations of single or double quotes resulted in the following traceback:

  File "<stdin>", line 1, in <module>
_tkinter.TclError: can not find channel named "hello,"

--- fracjackmac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!