Know any creative ways to interface Python with Tcl?
Asked Answered
R

3

18

Here's the situation. The company I work for has quite a bit of existing Tcl code, but some of them want to start using python. It would nice to be able to reuse some of the existing Tcl code, because that's money already spent. Besides, some of the test equipment only has Tcl API's.

So, one of the ways I thought of was using the subprocess module to call into some Tcl scripts.

  • Is subprocess my best bet?
  • Has anyone used this fairly new piece of code: Plumage? If so what is your experience (not just for Tk)?
  • Any other possible ways that I have not considered?
Ralfston answered 16/6, 2009 at 23:40 Comment(0)
L
20

I hope you're ready for this. Standard Python

import Tkinter
tclsh = Tkinter.Tcl()
tclsh.eval("""
    proc unknown args {puts "Hello World!"}
    }"!dlroW olleH" stup{ sgra nwonknu corp
""")

Edit in Re to comment: Python's tcl interpreter is not aware of other installed tcl components. You can deal with that by adding extensions in the usual way to the tcl python actually uses. Here's a link with some detail

Lashanda answered 17/6, 2009 at 0:10 Comment(8)
Thanks for the tip. How well does this work with other Tcl libraries that are installed?Ralfston
Hmm. come to think of it, the comment about it not being aware of other TCL installs may depend on the particulars of the platform. On windows binary install at least, there is no option to use another TCL. I wouldn't be surprised if a build from source can use any tcl available.Lashanda
Thanks again TokenMacGuy. That helps a lot. Even though I've been working with Python for quite some time, I've never looked into the Tkinter module(preffered wxPython myself). I just thought it was pure GUI widgets, instead of Tcl embedded into Python. That last link was very helpful as well.Ralfston
After all that, did you get the point that it's generally quick work to [load] any required binaries so extensions work as intended?Masker
Could you explain your tcl example? I am confused about the unbalanced quotation mark and the reverse commands.Noyes
@lalala: I can't seem to find the question that was my answer to; it's a palindrome that prints "hello world" without using comments.Lashanda
i also really meant commands not comments. You write: 'stup' which is the reverse of puts but I am just beginning tcl and find this confusing.Noyes
@lalala: it seems the question I'm thinking of is gone, since it was off topic for this site. It's close enough to on topic that i've somewhat recreated my answer here: codegolf.stackexchange.com/a/135028/72575Lashanda
C
3

This can be done.

http://wiki.tcl.tk/13312

Specificly look at the typcl extension.

Typcl is a bit weird... It's a an extension to use Tcl from Python. It doesn't really require CriTcl and could have been done in standard C.

This code demonstrates using Tcl as shared library, and hooking into it at run time (Tcl's stubs architecture makes this delightfully simple). Furthermore, Typcl avoids string conversions where possible (both ways).

Charente answered 16/6, 2009 at 23:55 Comment(2)
Interesting. I take it this is for Unix systems, and not Windows?Ralfston
Yes and no: I'm only aware of Typcl under Unix, but all its parts are cross-platform; it wouldn't surprise me if a little "relativization" of paths sufficed to make it work under Windows. In any case, the Wiki page already mentioned points to other variants of this theme that have been exercised under Windows.Masker
M
0

I've not used it myself, but SWIG might help you out:

http://www.swig.org/Doc1.1/HTML/Tcl.html

Mitrailleuse answered 16/6, 2009 at 23:50 Comment(2)
I think this is used for wrapping C or C++ code with higher level languages.Ralfston
Right: one could construct a solution with SWIG, but it's not nearly as direct a solution as "import Tkinter" or the Typcl-like projects.Masker

© 2022 - 2024 — McMap. All rights reserved.