Need help with Tkinter
Fredrik Lundh
fredrik at pythonware.com
Tue Dec 14 03:25:20 EST 1999
rodzilla2000 at my-deja.com wrote:
> Change the "Hello World" button a "Add Exit Button" that would put
> another button on the screen to allow exiting the program.
>
> Add the additional exit button...
>
> Here's the code:
>
> =========START OF CODE
> # Button test...
>
> import sys
> from Tkinter import *
>
> #main routine
> def main():
> root=Tk()
> btnAddButton=Button(root)
> btnAddButton['text']='Add Exit Button'
> btnAddButton['command']=subAddButton(root) # See below
this *calls* the subAddButton, and assigns the return
value as the button command. Tkinter should really
complain about that, but it doesn't -- after all, your
function might be returning a valid Tcl command...
try changing:
> btnAddButton['command']=subAddButton(root) # See below
to:
def cb(root=root):
subAddButton(root)
btnAddButton['command'] = cb
also consider using keyword syntax to initialize the widgets. see
http://www.pythonware.com/library/tkinter/introduction/widget-configuration.htm
for more info.
> btnAddButton.pack()
>
> #start mainloop
> root.mainloop
>
> def subAddButton(parent):
> btnExitButton=Button(parent)
> btnExitButton['text']='Exit Program'
> btnExitButton['command']=subExit #see below
> btnExitButton.pack()
>
> def subExit():
> sys.exit(0)
>
>
> #execute the main routine
> main()
</F>
More information about the Python-list
mailing list