I'm using tkinter and this problem bothers me a lot. My goal:
- Create several buttons
- When clicking a specific button, change the text shown on that button
I tried the codes below, but no matter what button I clicked, only the last one got changed.
import tkinter as tk
list1 = []
def update_btn_text(index):
list1[index].set('new')
for i in range(10):
btn_text = tk.StringVar()
list1.append(btn_text)
btn = tk.Button(root, textvariable=list1[i], command=lambda: update_btn_text(i))
btn_text.set('button' + str(i+1))
btn.pack()
root.mainloop()
I guess the problem is because of the i value, but how can I fix it? Thanks a lot!