0
votes

I am struggling with running python script in tcl. 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.

Let's pretend it is python script:

import sys

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

print(result)

and tcl one:

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. 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.

My question is, is there possibility to pass variables "more directly"? E.g 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 :)

Can anyone help?

Greetings!

1
Do you have to handle spaces in the returned values? - Shawn
@Shawn I don't think so. Numbers are the most important for me. - Kuba Nowak

1 Answers

0
votes

Skip the eval, and use

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

with a python script that prints out space-separated values:

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.