Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example for accessing Tcl functions from Python

Tags:

python

tcl

elmer enables one to run Python code from Tcl. What about 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.

like image 698
Sridhar Ratnakumar Avatar asked Mar 25 '10 21:03

Sridhar Ratnakumar


People also ask

What is Tcl for Python?

Tcl is a dynamic interpreted programming language, just like Python. Though it can be used on its own as a general-purpose programming language, it is most commonly embedded into C applications as a scripting engine or an interface to the Tk toolkit.

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.

Which is faster Tcl or Python?

Tcl. Like Python, Tcl is usable as an application extension language, as well as a stand-alone programming language. However, Tcl, which traditionally stores all data as strings, is weak on data structures, and executes typical code much slower than Python.


3 Answers

This is a rather old thread, but I recently stumbled on Tkinter.Tcl() which gives you direct access to a Tcl interpreter in python without having to spawn a Tk GUI as Tkinter.Tk() requires.

An example... suppose you have a Tcl file (foo.tcl) with a proc called main that requires an single filename as an argument... main returns a string derived from reading foo.tcl.

from Tkinter import Tcl

MYFILE = 'bar.txt'
tcl = Tcl()
# Execute proc main from foo.tcl with MYFILE as the arg
tcl.eval('source foo.tcl')
tcl_str = tcl.eval('main %s' % MYFILE)
# Access the contents of a Tcl variable, $tclVar from python
tcl.eval('set tclVar foobarme')
tclVar = tcl.eval('return $tclVar')

I haven't found another way to access Tcl objects from python besides through a return value, but this does give you a way to interface with Tcl procs. Furthermore, you can export python functions into Tcl as discussed in Using Python functions in Tkinter.Tcl()

like image 86
Mike Pennington Avatar answered Sep 21 '22 02:09

Mike Pennington


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.

like image 44
Lucas S. Avatar answered Sep 22 '22 02:09

Lucas S.


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...

like image 43
schlenk Avatar answered Sep 21 '22 02:09

schlenk