Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Call TCL Procedure using Python

Using below code i can able to call all procedure in the Proc.tcl file ,but i want to call individually the procs like sum or sub ,Please let me know any other possibility to call it

My proc file program,

 puts "hello"
    proc sum {a b} {

     set c [expr $a + $b]
     puts "Addition: $c "
    }




  proc sub {a b} {

     set c [expr $a - $b]
     puts "Substraction: $c "
    }

My Main file program,

 import Tkinter
    import os
    r=Tkinter.Tk()
    r.tk.eval('source proc.tcl')
like image 323
Anub Avatar asked May 08 '13 11:05

Anub


People also ask

How do you call a TCL script in Python?

Making Tcl Calls As Tcl is a string-interpreted language where everything is a string, evaluating Tcl calls within Python is effectively the same as evaluating a string object containing Tcl code using the interpreter's eval() function.

Why is TCL included in Python?

Tcl is bundled into Python in order to make Tk available as the Python module, Tkinter. Beginning at version v1. 5.2, includes IDLE, an integrated development environment for Python that requires Tkinter. Python 2.4 not only supports Tk on Unix, but Tk on Windows and Macintosh platforms as well.

What is procedure in TCL?

Procedures are nothing but code blocks with series of commands that provide a specific reusable functionality. It is used to avoid same code being repeated in multiple locations. Procedures are equivalent to the functions used in many programming languages and are made available in Tcl with the help of proc command.


3 Answers

Just carry on as you are:

>>> import Tkinter
>>> r = Tkinter.Tk()
>>> r.tk.eval('proc sum {a b} {set c [expr {$a + $b}]; puts "Sum $c"; return $c}')
''
>>> r.tk.eval('sum 2 5')
Sum 7
'7'

So in your case, having sourced the tcl file you can just do r.tk.eval("sum 5 5") to call that procedure.

Note: always brace expr expressions in tcl. As in my example above.

like image 180
patthoyts Avatar answered Oct 02 '22 17:10

patthoyts


I do not know tcl, but this looks logical:

import tkinter
r=tkinter.Tk()
r.tk.eval('source proc.tcl')
r.tk.eval('sum 1 2')
r.tk.eval('sub 1 2')

>>> hello
>>> Addition: 3 
>>> Substraction: -1
like image 20
kalgasnik Avatar answered Oct 02 '22 18:10

kalgasnik


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 "Substraction: $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:

Addition: 13

Substraction: 11

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.

like image 42
Hai Vu Avatar answered Oct 02 '22 17:10

Hai Vu