來自: http://liangqiu.blog.51cto.com/4461709/786898php
2012-02-22 16:45:11python
今天遇到一個問題,一個設備的接口API都是tcl腳本形式的,可是個人code都是python的,而後python須要調用tcl。如下是簡單的解決方法。函數
先寫一個tcl腳本(求階乘)helloworld.tcl:spa
#!usr/bin/tclshcode
proc Factorial {n} {blog
if {$n<=1} {接口
return 1get
}it
return [expr $n*[Factorial [expr $n-1]]]class
}
而後是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