lambda

Laura Creighton lac at cd.chalmers.se
Wed May 23 18:31:43 CEST 2001


I use lambda for things like this:

if __name__ == '__main__':
    root = Tkinter.Tk()
    w = Tkinter.Button(root, text='This is a very green button', width= 30, fg='green')
    
    buttonList = (
        ['show', lambda x = w: x.grid(row=0, col=0, columnspan= 5)],
        ['pink', lambda x = w: x.configure(fg='pink')],
        ['yellow', lambda x = w: x.configure(bg='yellow')],
        ['Exit', root.destroy],
        )
    column = 0
    for txt, cmd in buttonList:
        button = Tkinter.Button(root, text = txt, command = cmd)
        button.grid(row=1, col=column, sticky = 'w')
        column = column + 1
    root.mainloop()

If I had just used w.grid up there I would get a yellow button with pink
text.   This form is concise.  What do the lambda dislikers suggest I do
instead?  (This is a serious question)

Laura Creighton




More information about the Python-list mailing list