发布时间:2019-09-22 07:47:28编辑:auto阅读(2667)
今天遇到一个问题,一个设备的接口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
49788
49018
39705
36750
31145
27977
26941
21747
21658
20012
106°
741°
1321°
1259°
1121°
2256°
1340°
1952°
2204°
1712°