Python, Tkinter and Tcl (newbie alert)

Randall Hopper aa8vb at yahoo.com
Thu May 20 11:14:19 EDT 1999


John Huber:
 |I could just run the Tcl script using os.system() (I think), but seeing how
 |Tkinter is so closely related to Tcl, I was wondering:):
 |
 |When Python uses Tkinter, is it really running a Tcl shell in some form? and
 |if so, is it possible to execute Tcl code in this shell, something akin to
 |importing modules?

You get cleaner-looking code by using Tkinter, so I'd recommend considering
that route.  And it's pretty simple if you know Tk.

But yes, Tcl/Tk is still there under the hood.  You can poke at the
interpreter if you want:

% python
>>> from Tkinter import *
>>> root = Tk()
>>> root.tk.call( "button", ".button", "-text", "Quit", "-command", "exit" )
>>> root.tk.call( "pack", ".button" )
>>> root.mainloop()


Alternatively (dangerous: will break if you have spaces embedded in quotes):

% python
>>> from Tkinter import *
>>> root = Tk()
>>> apply( root.tk.call, split( "button .button -text Quit -command exit" ) )
>>> apply( root.tk.call, split( "pack .button" ) )
>>> root.mainloop()

But here's the Tkinter form.  Simpler:

% python
>>> from Tkinter import *
>>> import sys
>>> root = Tk()
>>> btn = Button( text="Quit", command=sys.exit )
>>> btn.pack()
>>> root.mainloop()


Randall




More information about the Python-list mailing list