Turning Tcl scripts into Python

Lightning
EuroTcl2018

Talk
Munich

Version 1.7
July 2018

Turning Tcl scripts into Python

… in just two minutes

© 2018 Michael Kaelbling, Siemens AG. All rights reserved. Unrestricted

We can …

Convert a Tcl script of any size into a valid Python script in just two minutes.

A one-line example

hello.tcl

 puts "Greetings from Tcl [info patchlevel]"
 > tclsh hello.tcl
Greetings from Tcl 8.6.7

What support is available?

Let's look at our Python installation.

Windows' Python installation

  > dir D:\Python36
… 2017-01-24 06:06 python.exe
… 2017-08-07 16:34 <DIR> tcl …

Drill down

  > dir D:\Python36\tcl
… 2017-08-07 16:35 <DIR> tcl8.6
… 2017-08-07 16:35 <DIR> tk8.6 …

Why does Python ship Tcl?

Because of Tk, which Python provides via its Tk interface package:  tkinter

Tcl's killer apps

Since it does ship Tcl …

Write a Python wrapper: hello.py

 import sys ; import tkinter
 print("Greetings from Python {0}.{1}.{2}".
       format(*sys.version_info))
 gui = tkinter.Tk()
 gui.tk.eval(open('hello.tcl', 'r').read())
# if a Tk application, add: gui.mainloop()

We now have valid Python

 > python3 hello.py
Greetings from Python 3.6.0
Greetings from Tcl 8.6.7

Success!


Works for Python 2, if you capitalize Tkinter and use print statements.

Motivation


{*}"Team Tcl"

Buzzwords

See Also

Python 2.7

 import Tkinter
 gui = Tkinter.Tk()
 gui.tk.eval(open('hello.tcl', 'r').read())
 if False: gui.mainloop()
      
 > python2 hello2.py
 Greetings from Tcl 8.5.15

Python 3.6

 import tkinter
 gui = tkinter.Tk()
 gui.tk.eval(open('hello.tcl', 'r').read())
 if False: gui.mainloop()
      
 > python3 hello3.py
 Greetings from Tcl 8.6.6