elmer使人能够从 Tcl 运行 Python 代码。反过来呢?谁能用 Python 举个例子?
更新:我的意思是,能够访问 Tcl 对象、调用 Tcl 函数等,而不是简单地运行一些 Tcl 代码。
最佳答案
这是一个相当古老的线程,但我最近偶然发现了 Tkinter.Tcl()
,它使您可以直接访问 python 中的 Tcl 解释器 ,而不必生成 Tk GUI 作为 Tkinter.Tk()
需要。
举个例子...假设您有一个 Tcl 文件 (foo.tcl
),其中包含一个名为 main
的过程,它需要一个文件名作为参数... main
返回从读取 foo.tcl
派生的字符串。
from Tkinter import Tcl
MYFILE = 'bar.txt'
tcl = Tcl()
# Execute proc main from foo.tcl with MYFILE as the arg
tcl.eval('source foo.tcl')
tcl_str = tcl.eval('main %s' % MYFILE)
# Access the contents of a Tcl variable, $tclVar from python
tcl.eval('set tclVar foobarme')
tclVar = tcl.eval('return $tclVar')
除了通过返回值之外,我还没有找到从 python 访问 Tcl 对象的另一种方法,但这确实为您提供了一种与 Tcl proc 交互的方法。此外,您可以将 python 函数导出到 Tcl,如 Using Python functions in Tkinter.Tcl() 中所述。
关于python - 从 Python 访问 Tcl 函数的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2519532/