发布时间:2019-09-22 07:47:28编辑:auto阅读(2622)
今天遇到一个问题,一个设备的接口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
49366
48558
39257
36341
30756
27568
26563
21383
21239
19586
226°
465°
451°
549°
895°
619°
1295°
1433°
1224°
1210°