How to get text from dynamically created buttons?
问题 I want my buttons when i click on them to return the text attribute of them. I've used a for loop to dynamically create the buttons but when i do this the btn variable gets stuck on the last created button meaning every button returns the same text value. listWords = ("Car","Train","Bus","Bike") var = 0 def getdef(): print(btn['text']) for word in listWords: btn = Button(window, text=word,command=getdef) btn.grid(column=var, row=0) var = var + 1 This code produces 4 buttons: Image of buttons