[Tutor] Python - TCL Interaction

Alan Gauld alan.gauld at blueyonder.co.uk
Wed Sep 1 22:38:31 CEST 2004

> I am using Tkinker to interface Python to TCL. I am able 
> to call tcl APIs, 

While this is possoble its not really a "documented feature" 
of Tkinter. ie it's not normal practice!

> Now, the problem is I have to pass a two dimensional list 

Whatever you pass to tk.eval needs to be in Tcl speak.

arr = [["apple", "mango", "orange"], ["rose", "jasmine",
root.tk.eval('test_tcl_func %s', %(list))

So you can't pass a Python list as a Python list, it will 
be interpreted as a Tcl bracketed expression. Instead you 
will need to convert the Python list into a Tcl style list 
expressed as a string.

So the question is how would you create a multi dimensional 
array/list in Tcl? How do you convert a Python list into that 
structure? It's basically a string formatting exercise.

So I'd aim to do it in stages:

Basic Tcl List building code:

"set L [list 1 2 3]"


L1 = [1, 2, 3]
L2 = [4, 5, 6]
tclstr = 'set L1 [list '
for elem in L1:
   tclstr += (str(elem) + ' ')
tclstr2 = "sel L2 [list "
for elem in L2:
   tclstr += (str(elem) + ' ')

tclstr = tclstr1 + ';' + tclstr2 + ';' + 'set L [list L1 L2]'


Or something like that, I haven't tested it :-)

Would it really be much harder to convert the Tcl code to Python?

Alan G.

More information about the Tutor mailing list