If you don't need the power of Tkinter, you can restructure proc.tcl a little and call the proc
via subprocess
:
proc.tcl:
proc sum {a b} {
set c [expr $a + $b]
puts "Addition: $c "
}
proc sub {a b} {
set c [expr $a - $b]
puts "Subtraction: $c "
}
eval $argv; # NOTE 1
caller.py:
import subprocess
import shlex
def tcl(command):
command_line = shlex.split(command)
output = subprocess.check_output(command_line)
return output
print(tcl('tclsh proc.tcl sum 5 8'))
print(tcl('tclsh proc.tcl sub 19 8'))
Output of caller.py:
b'Addition: 13 \n'
b'Subtraction: 11 \n'
Discussion
Note 1: In the Tcl script, the line eval $argv
takes what on the command line and execute it. It does not provide error checking at all, so potentially is dangerous. You will want to check the command line for malicious intention before executing it. What I have here is good for demonstration purpose.
The function tcl
in caller.py takes a command line, split it, and call proc.tcl to do the work. It collects the output and return it to the caller. Again, for demonstration purpose, I did not include any error checking at all.
Update
If you are using python3 (most of us do), then to convert output from bytes to string, try
output = subprocess.check_output(command_line, encoding="utf-8")