[Tutor] Access a .tcl file in python

Alan Gauld alan.gauld at yahoo.co.uk
Mon Feb 13 20:17:39 EST 2017


On 13/02/17 19:37, Lily ANTONY wrote:

> I have a .tcl file.Does anyone know how to load a .tcl file in to python?I
> need to call the .tcl file in
> a python program..Thanks!

In general you can't run code for one language in a
program written in another language. (There are a few
exceptions, usually where one language is written in
the other).

The nearest you can usually get is to execute the
Tcl program using the tcl interpreter (tclsh) from
the OS using a module like subprocess.

However, Tcl and Python do have a connection  via
the Tkinter module and you can call tcl code
using Tkinter like so(Python v3):

import tkinter
tcl = tkinter.Tcl()
tcl.eval("""
puts [expr 4+5]  # or any other arbitrary tcl
""")

So to execute a Tcl file you can read the contents
into a string and then execute that string. Note that
the return value will always be a string.

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list