0

I have finished program in Python to work, but now I'm trapped. They want me to do input in TCL, yep, so I need to solve that problem ASAP. The TCL script will just say what function he wants to call and what values he want to use. So the TCL script will just call Python, the Python will do that:

function.name(self)
(enter values f.e. 150, 200, 5)

and in Python that will just fill the input inside function (X = 150, Y = 200, timeout = 5):

def Press(self):
    self.X = int(input("X = "))
    self.Y = int(input("Y = "))
    self.timeout = int(input("How many seconds? ")
    time.sleep(self.timeout)
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • 1
    You aren't going to call Python functions directly, but tcl can certainly invoke Python programs, pass command line arguments, and parse the results that the Python script prints. – Tim Roberts Apr 04 '22 at 06:16
  • So what I have to do? – Martin Trnka Apr 04 '22 at 06:51
  • 2
    What's your general plan for how the two languages will be arranged with respect to each other? Bigger picture needed for correct solution to be offered! – Donal Fellows Apr 04 '22 at 07:37

3 Answers3

2

You can call Python code from Tcl using the tohil library.

package require tohil

tohil::exec {
    def Press(X, Y, timeout):
      print('X =', int(X))
      print('Y =', int(Y))
      print('timeout =', int(timeout))
}   

tohil::call Press 150 200 5

If you turned your Python code into a module, you can simply do:

package require tohil

tohil::import foo 
tohil::call foo.bar 150 200 5

The reverse is also possible (calling Tcl code from Python). See the tohil documentation for details.

Schelte Bron
  • 4,113
  • 1
  • 7
  • 13
1

The simplest way of connecting Tcl and Python is to just call one using the command line running of the other. For example:

exec python -c "import foo; foo.bar(150, 200, 5)"

It's probably better if we wrap that up:

proc callPython {module function args} {
    set pya ""
    foreach a $args {
        if {$a eq "True" || $a eq "False" || $a eq "None"} {
            # Well-known special constants
            append pya $a ","
        } elseif {[string is integer -strict $a] || [string is double -strict $a]} {
            # Numbers
            append pya $a ","
        } else {
            # Assume everything else is a string
            # This format passes pretty much arbitrary stuff
            append pya {r"""} $a {""",}
        }
    }
    # Fortunately, Python's happy about having an extra comma
    exec python -c "import $module; $module.$function($pya)"
}

See also: Run function from the command line

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
0

If your main program is Python and you want to run a Tcl script in Python then can use _tkinter for that, eg

import _tkinter as _tk

s = """
puts "put your Tcl script in here"
"""
tcl = _tk.create()
tcl.call('eval',s)

tcl.setvar() and tcl.getvar() can be used to connect variables between Python and Tcl.

So this is the inverse to running Python from Tcl which is already answered. (Just put in this answer for interests sake)

InhirCode
  • 338
  • 1
  • 4
  • 5