发布时间:2019-09-22 07:47:28编辑:auto阅读(2583)
今天遇到一个问题,一个设备的接口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
48920
48031
38789
35918
30338
27118
26123
20956
20778
19137
648°
728°
699°
691°
666°
632°
728°
814°
920°
1126°