Python, Tkinter and Tcl (newbie alert)

Greg McFarlane gregm at iname.com
Thu May 20 23:46:24 EDT 1999


It is easy to give complete tcl scripts to the Tkinter tcl interpreter.
For example:

======================================================================
import Tkinter
root = Tkinter.Tk()
tclScript = """
    # Here is a tcl script.  Three cheers for tcl!
    label .label -text "Honk if you think tcl is the best"
    pack .label
    button .button -text "Go away please" -command {destroy .}
    pack .button
"""
root.tk.call('eval', tclScript)
root.mainloop()
======================================================================

In this sense, python is a complete superset of tcl :^)=


On 20 May, Randall Hopper wrote:
> 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
> 
> 
> 
> 

-- 
Greg McFarlane:    INMS Telstra Australia (gregm at iname.com)
Today's forecast:  Sunny, with occasional cloudy periods and a chance
		   of rain in some areas.




More information about the Python-list mailing list