I'm building an application with a lot of buttons, so I use a list of them and a for-loop to bind a function to each of them that prints the button's text when clicked. When I bind it to each button individually everything works fine, but when I use the for-loop every button only prints the text of the last item in the button list (which is "3" in this case).
import tkinter as Tk
from tkinter import *
win = Tk()
b1 = Button(win, text="1")
b1.grid(row=0)
b2 = Button(win, text="2")
b2.grid(row=0,column=1)
b3 = Button(win, text="3")
b3.grid(row=0,column=2)
button_list = [b1,b2,b3]
def printText(item):
print(item["text"])
for button in button_list:
button.bind("<Button-1>",lambda a:printText(button))
root.mainloop()
From what I've seen in similar questions this has something to do with lambda, however I'm not familiar with the lambda function and I'm struggling to understand how to go about fixing this.