python - 从 Python 访问 Tcl 函数的示例

标签 python tcl

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/

相关文章:

python - 奇怪的 Python 线程行为

python - Pyserial 读取循环需要多个输入来注册

python - 如何将 yapf 应用于目录下的每个 python 文件?

delphi - 如何在TCL中使用Delphi编译的DLL?

python - 如何在默认的 Sphinx 主题上使用自定义 CSS?

python - Flask-restful 基本认证

c - 使用 google translate 翻译基于 printf 的字符串

arraylist - 如何在tcl中获取数组列表

regex - 如何打印在 TCL 变量中多次出现的单词(URL 名称)?

tcl - 尝试在 TCL/Expect 中使用 put 时出错