从 Python 访问 Tcl 函数的示例
elmer 使人们能够从 Tcl 运行 Python 代码。反过来呢?谁能用 Python 举一个例子吗?
更新:我的意思是,能够访问 Tcl 对象、调用 Tcl 函数等,而不是简单地运行一些 Tcl 代码。
elmer enables one to run Python code from Tcl. What about the other way around? Could anyone give an example in Python?
Update: by this I mean, being able to access Tcl objects, invoke Tcl functions, etc. instead of simply running some Tcl code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个相当老的线程,但我最近偶然发现了 Tkinter.Tcl(),它可以让你直接访问 python 中的 Tcl 解释器,而不必像 Tkinter 一样生成 Tk GUI .Tk() 需要。
举个例子...假设您有一个 Tcl 文件 (
foo.tcl
),其过程名为main
,需要单个文件名作为参数...main
返回从读取foo.tcl
派生的字符串。除了通过返回值之外,我还没有找到从 python 访问 Tcl 对象的另一种方法,但这确实为您提供了一种与 Tcl 过程交互的方法。此外,您可以将 python 函数导出到 Tcl 中,如 在中使用 Python 函数中所述Tkinter.Tcl()
This is a rather old thread, but I recently stumbled on
Tkinter.Tcl()
which gives you direct access to a Tcl interpreter in python without having to spawn a Tk GUI asTkinter.Tk()
requires.An example... suppose you have a Tcl file (
foo.tcl
) with a proc calledmain
that requires an single filename as an argument...main
returns a string derived from readingfoo.tcl
.I haven't found another way to access Tcl objects from python besides through a return value, but this does give you a way to interface with Tcl procs. Furthermore, you can export python functions into Tcl as discussed in Using Python functions in Tkinter.Tcl()
您可以查看 python tkinter 标准模块,它是到 Tk UI 的绑定,但在后台执行 Tcl 代码。
You can take a look at python tkinter standard module, it is a binding to Tk UI, but that executes Tcl code in the background.
虽然理论上可以通过提供一些透明地包装 C 级 Tcl_Obj* 的自定义 Python 对象类型来实现,但一般情况只是来回交换字符串,因为 Tcl 的 EIAS 语义可以自由地消除所有其他内部表示。
所以,是的,你可以比字符串做得更好,但通常不值得。 Tkinter 已经内置了一些可能的优化,所以尝试窃取......
While it might be possible in theory by providing some custom Python object types that wrap C level Tcl_Obj*'s transparently, the general case is just exchanging strings back and forth as Tcl's EIAS semantics can shimmer away every other internal representation freely.
So yes, you could do better than strings, but its usually not worth it. Tkinter already has a few of the possible optimizations built in, so try to steal there...
这?
This?