简体   繁体   English

如何将变量从 TCL 传递给 python 并将 python output 返回给同一个 TCL 脚本?

[英]How to pass variable to python from TCL and return python output to the same TCL script?

I am struggling with running python script in tcl.我正在努力在 tcl 中运行 python 脚本。 I want to run tcl script, call python script with tcl variables as parameters, than process these value with python and pass them to the same tcl script for next processing.我想运行 tcl 脚本,以 tcl 变量作为参数调用 python 脚本,然后使用 python 处理这些值并将它们传递给相同的 tcl 脚本以进行下一次处理。

Let's pretend it is python script:假设它是 python 脚本:

import sys

def f(a,b):
    return "set lista {" + a + " "+ b +"}"
    
result = f(sys.argv[1],sys.argv[2])

print(result)

and tcl one:和 tcl 之一:

set num1 10
set num2 20

set output [exec python "python.py" $num1 $num2]

eval $output

puts [lindex $lista 0]
puts [lindex $lista 1]

I have used eval statement and it works quite well for such small list.我使用过 eval 语句,它对这么小的列表非常有效。 In fact I am going to have longer list or float values and strings that I would like to separate each other.事实上,我将有更长的列表或浮点值和字符串,我想将它们彼此分开。 Now this solution, using eval , isn't convenient.现在这个使用eval的解决方案并不方便。

My question is, is there possibility to pass variables "more directly"?我的问题是,是否有可能“更直接地”传递变量? Eg each value as separate variable or even split list, one float values and other for strings.例如,每个值作为单独的变量或什至拆分列表,一个浮点值和另一个字符串。

I know there is tkinter library, but I am beginner and I am still learning... and to be honest I am not sure how I should use it:)我知道有tkinter图书馆,但我是初学者,我还在学习......老实说,我不确定我应该如何使用它:)

Can anyone help?谁能帮忙?

Greetings!问候!

Skip the eval , and use跳过eval ,然后使用

set lista [split [exec python python.py $num1 $num2]]

with a python script that prints out space-separated values:使用打印出空格分隔值的 python 脚本:

import sys

def f(a,b):
    return str(a) + " " + str(b)
    
result = f(sys.argv[1],sys.argv[2])

print(result)

This works as long as you don't have spaces embedded in the values.只要您没有在值中嵌入空格,这就有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM