Tkinter/Pmw callback question

Rick Pasotto rickp at telocity.com
Sun Feb 25 17:43:11 EST 2001


On Sun, 25 Feb 2001 21:24:11 GMT in comp.lang.python, David Lees wrote:
> I am trying to learn Tkinter and Pmw and am modifying demo Pmw code
> (EntryField.py).  I want to call a Python function that I have defined
> when I push a button and am having problems with the callback.  As
> long as the function has no arguments all is well, but I am unable to
> do a callback on a function that has arguments.  For example this
> works:
> 
>     RunButton = Tkinter.Button(root, text = 'Run',command=foo)
> 
> where foo has no arguments, but when I write:
> 
>     RunButton = Tkinter.Button(root, text = 'Run',command=barf('junk'))
> 
> things do not work correctly.  The 'barf' function seems to execute
> when the program starts up, but not when I click the Run button.
> 
> What goes on here and what is the correct way to do call backs?

I'm just learning myself, but I think what I'm going to say is correct.

'command=' takes the *name* of a function. Adding parens changes the
*name* to an actual function call. The way to get around this is to use
the nameless name 'lambda' for the name of the function and *it* can
call the function you really want. So:

   rb = Button(root, text='Run', command='lambda x="junk":barf(x)')

-- 
"Every great advance in natural knowledge has involved
the absolute rejection of authority."
		-- Thomas Henry Huxley
		   Rick Pasotto email: rickp at telocity.com



More information about the Python-list mailing list