I have a problem with my code. I'm trying to create several buttons in a loop and assign them a value from x [i], but the value of "i" changes in the command of all buttons.
import tkinter as tk
def buttons():
x = [11, 22, 34, 45, 56]
for i in range(len(x)):
button.menu.add_cascade(label=i+1, command=lambda: test(x[i]))
def test(text):
print(text)
window = tk.Tk()
window.geometry("300x130")
button = tk.Menubutton(window, text='Button', relief=tk.RAISED)
button.pack()
button.menu = tk.Menu(button, tearoff=0)
button['menu'] = button.menu
buttons()
window.mainloop()