I made a code that looks like this :
from tkinter import *
root = Tk()
def Click(val):
print("Value: "+val)
var_str = ["1","2","3","4","5"]
#I want var_str to be expandable
var_list = []
for x in range(0,len(var_str)):
var_list.append(x)
var_list[x] = StringVar()
var_list[x].set(var_str[x])
#I basically make a set of StringVar that contains strings in var_str
b = []
for x in range(0,len(var_list)):
Button(root, textvariable=var_list[x], command=lambda: Click(var_list[x].get())).pack()
#I make the set of StringVar into a set of button
root.mainloop()
I wanted to make a set of buttons that print their value based on the value they display
I used Stringvar because I wanted the value to be changeable later with a button
The problem is why am I keep getting "5" whenever I pressed other numbers? Is there anything wrong with the code?