[Tutor] tkinter buttons giving me problems

Rick Pasotto rick@niof.net
Wed, 9 May 2001 23:22:13 -0400


On Wed, May 09, 2001 at 03:58:20PM -1000, Brett wrote:
> I'm trying to figure out Tkinter, but I'm having trouble with 
> buttons.  When if set a button's command, it calls the command  without 
> being clicked.  The following is an idle transcript:
> 
>  >>> from Tkinter import *
>  >>> x=0
>  >>> def func():
> 	global x
> 	x=1
> 
>  >>> root=Tk()
>  >>> x
> 0
>  >>> button=Button(root,command=func(),text='button').pack()
>  >>> x
> 1
> 
> Is this supposed to happen?  Is there a way to bind a button to a command 
> without executing the command?

'command=' takes the *name* of a function, not a function call.

Drop the parentheses and all should be well. What you want is:

>>> button=Button(root,command=func,text='button')
>>> button.pack()

The change to the pack() is because what you had results in the return
value of pack() being stored in the 'button' variable. I suspect you'd
rather have a reference to the button itself.

-- 
Certain nations seem particularly liable to fall prey to
governmental plunder. They are those in which men, lacking faith
in their own dignity and capability, would feel themselves lost if
they were not governed and administered every step of the way.
	-- Frédéric Bastiat (1801-1850)
    Rick Pasotto    rickp@telocity.com    http://www.niof.net