On Thu, 06 Dec 2007 23:04:56 -0800, robbie wrote: > This second example doesn't work. The only difference is that I gave > the salutation function a parameter and tried to pass a string as the > parameter from the button. It appears to call the function once for > each window as they're being created, but doesn't do anything when the > buttons in the windows are pressed. > > from Tkinter import * > root = Tk() > > trees = [('The Larch!', 'light blue'), > ('The Pine!', 'light green'), > ('The Giant Redwood!', 'red')] > > def salutation(j=""): > print j + " Something!" > > for (tree, color) in trees: > win = Toplevel(root) > win.title('Sing...') > win.protocol('WM_DELETE_WINDOW', lambda:0) > win.iconbitmap('py-blue-trans-out.ico') > msg = Button(win, text='Write Something', > command=salutation(tree))
You are calling the function here. Parenthesis after a callable like a function means "call it *now*", so you are calling `salutation()` and bind the *result* of that call to `command`. An idiomatic solution is to use a ``lambda`` function:: msg = Button(win, text='Write Something', command=lambda t=tree: salutation(t)) In Python 2.5 there's an alternative way with the `functools.partial()` function: from functools import partial # ... msg = Button(win, text='Write Something', command=partial(salutation, tree)) Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list