while coding a GUI with Tkinter I've encountered a problem.
I'm generating a series of buttons and label based on a dynamic list, following there's an example explaining my problem:
root = tk.Tk()
root.geometry('600x500')
main_frame = tk.Frame(root, bg='grey').grid(row=0,column=0)
my_list = ['a', 'b', 'c', 'd', 'e', 'f']
for x in range(len(my_list)):
tk.Button(text='CHECK', command=lambda:print('The value is {}'.format(my_list[x]))).grid(row=x,column=0)
tk.Label(text=my_list[x]).grid(row=x,column=1)
The problem is evident, what i want to do is to print the value corresponding at my_list[x] at the moment of its assignment, but while looping the value of x is reassigned and all the buttons print me the last string in my_list.
Is there a way to store a string value in a case like this? Or every time i should create n variables to store the string? Note that my_list length can vary.