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

John Edens jedens at cabinc.com
Thu Jun 12 19:56:41 CEST 2008


That is exactly what I was looking for - seems like something that should go into the wiki!

-----Original Message-----
From: Bob Greschke [mailto:bob at passcal.nmt.edu]
Sent: Thursday, June 12, 2008 11:52 AM
To: John Edens
Cc: tkinter-discuss at python.org
Subject: Re: [Tkinter-discuss] New member howdy And is there a better way to use Buttons ?


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

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




More information about the Tkinter-discuss mailing list