calling functions from Tk

Ivan Van Laningham ivanlan at callware.com
Sat Dec 25 10:30:44 EST 1999


Hi All--

scoobysnax5336 at my-deja.com wrote:
> 
> I'm trying to understand this.  I've cleaned it up as much as I can to
> illustrate my problem.  From my buttons, if I call a function with no
> value, it works fine.  When I call it with a value, it executes at
> runtime but not when the button is pressed.
> 

But that's exactly what you're telling it to do.

command=test

vs.

command=test('blah')

The first method tells Button that 'test' is the name of the function to
call when the button is pressed.  The second method says that the result
of running "test('blah') is the function to call when the button is
pressed.  So, when you create the button, it calls the test() function. 
Your test() function returns None (all functions without an explicit
return value return a None when they finish), so the result that is
stored in the command field is a None.  Thus, nothing happens when you
push the button.

There are other ways to specify arguments for callback functions. ...

> from Tkinter import *
> 
> def test(stuff='test'):
>         print stuff
> 
> root = Tk()
> 
> b=Button(root, text="Call test", width=8, command=test)
> b.pack(side=LEFT)
> 
> b=Button(root, text="Call test('blah')", width=12, command=test('blah'))
> b.pack(side=LEFT)
> 
> b=Button(root, text='Exit', width=6, command=root.quit)
> b.pack(side=LEFT)
> 
> root.mainloop()
> 

<python-does-what-you-tell-it-not-what-you-mean>-ly y'rs,
Ivan
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan at callware.com
ivanlan at home.com
http://www.pauahtun.org
See also: 
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps:  Cu Chi, Class of '70
Author:  Teach Yourself Python in 24 Hours
----------------------------------------------




More information about the Python-list mailing list