Tkinter/Pmw callback question

bernie bernie at pacific.net.hk
Wed Feb 28 18:45:47 CET 2001


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?
>
>

Maybe you can try this, as an alternative to lamba:

First define a Command class:
class Command:
    def __init__( self, func, *args, **kw):
        self.func = func
        self.args = args
        self.kw = kw

    def __call__( self, *args, **kw):
        args = self.args + args
        kw.update( self.kw)
        apply( self.func, args, kw)

Then:
RunButton = Tkinter.Button(root, text = 'Run',
        command=Command(barf, 'junk'))

This method will be handy when barf is long (e.g. it contains if ... else).

Bernie




More information about the Python-list mailing list