发布时间:2019-09-22 07:47:28编辑:auto阅读(2735)
今天遇到一个问题,一个设备的接口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
50400
49691
40299
37337
31742
28601
27537
22321
22314
20625
372°
980°
771°
727°
970°
847°
1465°
2747°
2480°
1906°