[Tkinter-discuss] New member howdy And is there a better way to use Buttons ?

Guilherme Polo ggpolo at gmail.com
Thu Jun 12 20:22:40 CEST 2008


On Thu, Jun 12, 2008 at 1:51 PM, Bob Greschke <bob at passcal.nmt.edu> wrote:
>
> On Jun 11, 2008, at 13:52, John Edens wrote:
>
>> Hi all, I've just started working with Python and Tkinter.
>>
>> One of the things I think I've noted is that Button commands do not pass
>> parameters.
>>
>> Is there a way around this?
>>
>> And is there a better way to code the following:
>>
>> I've written a small application that puts a Label in row 0 of a Frame and
>> puts 26 buttons labeled A – Z in the second row.
>>
>> When the user clicks on a button, the associated letter is displayed in
>> the label.
>>
>
> I use (stolen from several places in Python history):
>
> ######################
> # 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.

> then for a button you would do
>
> Button(Frame, text = "BLAH B", command = Command(doBlah, "B")).pack(side =
> LEFT)
> Button(Frame, text = "BLAH C", command = Command(doBlah, "C")).pack(side =
> LEFT)
> .
> .
> .
>
> Bob
>
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>



-- 
-- Guilherme H. Polo Goncalves


More information about the Tkinter-discuss mailing list