[Tutor] calling functions with buttons - why doesn't this work??
Sat, 28 Oct 2000 21:14:29 -0700 (PDT)
On Sun, 29 Oct 2000, wheelege wrote:
> I was trying to call a function from a button, and it wouldn't work.
> So I wrote this little thing and even it doesn't work. When it is run
> it executes hi() but not when the button is pressed. Help? As a side
> note, why isn't my window title 'yo'?
> f = Frame(root)
> b = Button(text='hi', command=hi())
It's a subtle problem: the problem is that the command taken by Button
needs to be a function. However, hi() will just call the hi() function,
returning whatever hi() does. You'll want to give your button the
function 'hi', not the result of what happens when you call 'hi'. To fix
this, you'll want:
b = Button(text='hi', command=hi)
It doesn't look like much of a change, but, semantically, it's a big
difference! Take a look at this interpreter session:
>>> x = hi()
## 'x' contains the None value, since 'hi()' doesn't
## explicitly return anything. Now let's contrast
## this with 'hi'.
>>> x = hi
<function hi at 0x81ad2cc>
Hope this clears things up.