发布时间:2019-09-22 07:47:28编辑:auto阅读(2541)
今天遇到一个问题,一个设备的接口API都是tcl脚本形式的,但是我的code都是python的,然后python需要调用tcl。以下是简单的解决方法。
先写一个tcl脚本(求阶乘)helloworld.tcl:
#!usr/bin/tclsh
proc Factorial {n} {
if {$n<=1} {
return 1
}
return [expr $n*[Factorial [expr $n-1]]]
}
然后是python调用tcl:
#!/usr/bin/python
from Tkinter import Tcl
#建立一个tcl的解释器
tcl = Tcl()
#导入tcl文件
tcl.eval('source helloworld.tcl')
#调用函数,tcl_str为返回值
tcl_str=tcl.eval('Factorial %d' % 10)
print tcl_str
上一篇: Python IP切换
下一篇: Python thread socket
48709
47731
38514
35713
30152
26889
25908
20761
20522
18918
309°
378°
417°
426°
411°
422°
466°
529°
655°
659°