知道将 Python 与 Tcl 交互的任何创造性方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1004434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Know any creative ways to interface Python with Tcl?
提问by Jay Atkinson
Here's the situation. The company I work for has quite a bit of existing Tcl code, but some of them want to start using python. It would nice to be able to reuse some of the existing Tcl code, because that's money already spent. Besides, some of the test equipment only has Tcl API's.
这是情况。我工作的公司有相当多的现有 Tcl 代码,但其中一些人想开始使用 python。能够重用一些现有的 Tcl 代码会很好,因为这些钱已经花掉了。此外,一些测试设备只有Tcl API。
So, one of the ways I thought of was using the subprocess module to call into some Tcl scripts.
所以,我想到的方法之一是使用 subprocess 模块调用一些 Tcl 脚本。
- Is subprocess my best bet?
- Has anyone used this fairly new piece of code: Plumage? If so what is your experience (not just for Tk)?
- Any other possible ways that I have not considered?
- 子流程是我最好的选择吗?
- 有没有人使用过这段相当新的代码:Plumage?如果是这样,您的经验是什么(不仅仅是 Tk)?
- 我还没有考虑过其他任何可能的方法吗?
回答by SingleNegationElimination
I hope you're ready for this. Standard Python
我希望你为此做好了准备。标准 Python
import Tkinter
tclsh = Tkinter.Tcl()
tclsh.eval("""
proc unknown args {puts "Hello World!"}
}"!dlroW olleH" stup{ sgra nwonknu corp
""")
Edit in Re to comment: Python's tcl interpreter is not aware of other installed tcl components. You can deal with that by adding extensions in the usual way to the tcl python actually uses. Here's a link with some detail
在 Re 中编辑评论:Python 的 tcl 解释器不知道其他已安装的 tcl 组件。您可以通过以通常的方式向 tcl python 实际使用的扩展添加扩展来解决这个问题。这是一个包含一些细节的链接
回答by Byron Whitlock
This can be done.
这是可以做到的。
Specificly look at the typcl extension.
具体看typcl扩展。
Typcl is a bit weird... It's a an extension to use Tcl fromPython. It doesn't really require CriTcl and could have been done in standard C.
This code demonstrates using Tcl as shared library, and hooking into it at run time (Tcl's stubs architecture makes this delightfully simple). Furthermore, Typcl avoids string conversions where possible (both ways).
Typcl 有点奇怪……它是从Python使用 Tcl 的扩展。它并不真正需要 CriTcl 并且可以在标准 C 中完成。
这段代码演示了如何使用 Tcl 作为共享库,并在运行时连接到它(Tcl 的存根架构使这变得非常简单)。此外,Typcl 尽可能避免字符串转换(两种方式)。
回答by user9755
I've not used it myself, but SWIG might help you out:
我自己没有使用过,但 SWIG 可能会帮助你: