[Tkinter-discuss] New member howdy And is there a better way to use Buttons ?
Bob Greschke
bob at passcal.nmt.edu
Tue Jun 17 00:48:11 CEST 2008
On Jun 12, 2008, at 12:22, Guilherme Polo wrote:
>>
>> ######################
>> # BEGIN: class Command
>> # LIB:Command():2006.110
>> # Pass arguments to functions from button presses and menu
>> selections! Nice!
>> # In your declaration: ...command = Command(func, args,...)
>> # Also use in bind() statements
>> # x.bind("<****>", Command(func, args...))
>> 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)
>> # END: class Command
>>
>
> You don't need all that machinery tho, something simpler like:
>
> def command(func, *args, **kw):
> def _wrapper(*wargs):
> return func(*(wargs + args), **kw)
>
> return _wrapper
>
> would work too. But considering that code is using "apply", I will
> take it as really old and outdated by now.
Ooo. Much better. I got mine from Grayson's book which says it
originally came from a Tim Evans post to the Python newsgroup.
Thanks!
Bob
More information about the Tkinter-discuss
mailing list