faqts : Computers : Programming : Languages : Python : Snippets : Tkinter

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

8 of 11 people (73%) answered Yes
Recently 6 of 9 people (67%) answered Yes

Entry

Python, Tkinter and Tcl (newbie alert)

Jul 5th, 2000 09:59
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 76, Randall Hopper


"""
Packages: gui.tkinter
"""
"""
 |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()
"""