使用参数从TCL脚本调用Python函数
我有一个Python脚本,比如说,使用参数从TCL脚本调用Python函数,python,tcl,Python,Tcl,我有一个Python脚本,比如说,sample.py,包含两个函数func1()和func2(): 如何从Tcl脚本调用此函数func2(),以及如何将参数传递给该函数?python代码需要此函数(我将其称为junk.py,并将其与Tcl脚本放在同一文件夹中) 名为test_Tcl.Tcl的Tcl文件: set out [exec python junk.py [lindex $argv 0]] puts $out 从命令行执行: tclsh test_tcl.tcl func1 输出为:
sample.py
,包含两个函数func1()
和func2()
:
如何从Tcl脚本调用此函数func2()
,以及如何将参数传递给该函数?python代码需要此函数(我将其称为junk.py,并将其与Tcl脚本放在同一文件夹中)
名为test_Tcl.Tcl的Tcl文件:
set out [exec python junk.py [lindex $argv 0]]
puts $out
从命令行执行:
tclsh test_tcl.tcl func1
输出为:
Called Function 1
最好的方法可能是看看它是否适合你。(在上下载链接;选择您需要的链接。)这样您就可以编写如下代码:
# Set up a python interpreter
package require tclpython
set py [python::interp new]
# Load a file in with your definitions
$py exec {
# This is Python code, embedded directly in Tcl
evalfile("sample.py")
}
# Evaluate a python expression (in this case, call an argument-less function)
set result [$py eval func2()]
# Dispose of the python interpreter
python::interp delete $py
你想让他们处于相同的过程中吗?是的,Donal Fellows。。。。如果在同一个过程中就更好了。。
Called Function 1
# Set up a python interpreter
package require tclpython
set py [python::interp new]
# Load a file in with your definitions
$py exec {
# This is Python code, embedded directly in Tcl
evalfile("sample.py")
}
# Evaluate a python expression (in this case, call an argument-less function)
set result [$py eval func2()]
# Dispose of the python interpreter
python::interp delete $py