Tkinter. Create multiple buttons with “different” command function

后端 未结 1 809
我在风中等你
我在风中等你 2021-02-05 20:56

first of all, sorry for the title, I couldn\'t find a better one.

The following code is a minimalized version of a problem I have in my Python program (I am a newbie btw

1条回答
  •  别那么骄傲
    2021-02-05 21:30

    Use default parameter to avoid late-binding issue (Otherwise i is bound when the lambda function is called, not when it is created):

    def start():
        buttons = []
        win = Tkinter.Tk()
        for i in range(5):
            b = Tkinter.Button(win, height=10, width=100, command=lambda i=i: onClick(i))
            b.pack()
            buttons.append(b)
    

    0 讨论(0)
提交回复
热议问题