[Tkinter-discuss] New member howdy And is there a better way to use Buttons ?
Bob Greschke
bob at passcal.nmt.edu
Thu Jun 12 18:51:37 CEST 2008
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