4
  1. I have a bunch of Python functions. Let's call them foo, bar and baz. They accept variable number of string arguments and does other sophisticated things (like accessing the network).

  2. I want the "user" (let's assume he is only familiar with Tcl) to write scripts in Tcl using those functions.

Here's an example (taken from Macports) that user can come up with:

post-configure {
    if {[variant_isset universal]} {
        set conflags ""
        foreach arch ${configure.universal_archs} {
            if {${arch} == "i386"} {append conflags "x86 "} else {
                if {${arch} == "ppc64"} {append conflags "ppc_64 "} else {
                    append conflags ${arch} " "
                }
            }
        }

        set profiles [exec find ${worksrcpath} -name "*.pro"]
        foreach profile ${profiles} {
            reinplace -E "s|^(CONFIG\[ \\t].*)|\\1 ${conflags}|" ${profile}

            # Cures an isolated case
            system "cd ${worksrcpath}/designer && \
                    ${qt_dir}/bin/qmake -spec ${qt_dir}/mkspecs/macx-g++ -macx \
                    -o Makefile python.pro"
        }
    }
}

Here, variant_issset, reinplace are so on (other than Tcl builtins) are implemented as Python functions. if, foreach, set, etc.. are normal Tcl constructs. post-configure is a Python function that accepts, well, a Tcl code block that can later be executed (which in turns would obviously end up calling the above mentioned Python "functions").

Is this possible to do in Python? If so, how?

from Tkinter import *; root= Tk(); root.tk.eval('puts [array get tcl_platform]') is the only integration I know of, which is obviously very limited (not to mention the fact that it starts up X11 server on mac).

4
  • Not all builds of Tk start X11 on OSX. Use a Carbon (or Cocoa) targeted build of that library instead... Commented Apr 23, 2010 at 22:06
  • @Donal, well the point is - it brings up some GUI window. If not X11, I get a jumping Python rocket icon in the Dock followed by a window titled 'tk'. This is not desired for an app that has got nothing to do with GUI. Commented Apr 23, 2010 at 23:50
  • I suspected that might happen anyway. Didn't claim it was an answer after all. Commented Apr 24, 2010 at 5:51
  • 2
    You can use the tcl interpreter without loading Tk like this: import Tkinter tcl = Tkinter.Tcl() result = tcl.eval(''' puts hello, world ''') Commented Apr 25, 2010 at 13:18

2 Answers 2

10

With a little experimentation I discovered you can do something like this to create a tcl interpreter, register a python command, and call it from Tcl:

import Tkinter

# create the tcl interpreter
tcl = Tkinter.Tcl()

# define a python function
def pycommand(*args):
    print "pycommand args:", ", ".join(args)

# register it as a tcl command:
tcl_command_name = "pycommand"
python_function = pycommand
cmd = tcl.createcommand(tcl_command_name, python_function)

# call it, and print the results:
result = tcl.eval("pycommand one two three")
print "tcl result:", result

When I run the above code I get:

$ python2.5 /tmp/example.py
pycommand args: one, two, three
tcl result: None
Sign up to request clarification or add additional context in comments.

4 Comments

Great! Also - both the arguments and return value of pycommand happens to be strings, which is obvious as Tcl represents values as strings. If you return a list ([1, "foo"]), for instance, from pycommand ... that would be converted to string when tcl.eval returns. The "None" that is being printed in your example is actually of string type (as shown by type(result)).
I just realized that code blocks ({...}) that are passed as mere strings to Python functions can simply be evaled again.
tcl.setvar can be used for populating the variables to be used by the Tcl code .. though I don't know how dotted values (${configure.universal_archs}) can be passed on.
Easily, the dot is not really special in Tcl variable names (it only is special to $ evaluation but not to [set]).
0

@Brian - I had to experiment in order to get the right result

from Tkinter import Tcl
tcl = Tcl()
result = tcl.eval(' puts "hello, world" ')

Note the placement of the single and double quotes. This gave me the expected output: hello, world

Any other combinations of single or double quotes resulted in the following traceback:

  File "<stdin>", line 1, in <module>
_tkinter.TclError: can not find channel named "hello,"

--- fracjackmac

1 Comment

What point are you trying to make? That if you give invalid Tcl code you get an error, or are you asking why you get that error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.