views:

156

answers:

3

I see that elmer makes it possible to run Python code from Tcl. But is it possible the other way around? Could anyone give an example in Python?

Update: by this I mean, being able to access Tcl objects, invoke Tcl functions, etc instead of simply running some Tcl code.

+4  A: 

You can take a look at python tkinter standard module, it is a binding to Tk UI, but that executes Tcl code in the background.

Lucas S.
Eg. from Tkinter import *; root= Tk(); root.tk.eval('puts [array get tcl_platform]');
Colin Macleod
@Colin - as the question reads "being able to access Tcl objects, invoke Tcl functions, etc..", shouldn't your example be written as to get reference to that Tcl array in Python (instead of retrieving the string representation of what is printed)?
Sridhar Ratnakumar
Well I just added a quick comment to give you the idea. Also I suspect the Python<->Tcl communication will probably have to be at the level of passing commands and getting results as strings anyway, I wouldn't expect them to be able to share data structures.
Colin Macleod
A: 

This?

import subprocess
subprocess.Popen( "tclsh someFile.tcl", shell=True )
S.Lott
No, please read the clarification I just added to the question.
Sridhar Ratnakumar
There is no need to use the shell in this case. `subprocess.Popen(["tclsh", "someFile.tcl"])` works fine.
Mike Graham
A: 

While it might be possible in theory by providing some custom Python object types that wrap C level Tcl_Obj*'s transparently, the general case is just exchanging strings back and forth as Tcl's EIAS semantics can shimmer away every other internal representation freely.

So yes, you could do better than strings, but its usually not worth it. Tkinter already has a few of the possible optimizations built in, so try to steal there...

schlenk