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


_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to