I run into small problem trying to create 0-9 number buttons. I want to define 10 buttons for numbers from 0 to 9 in single loop. Each of them is supposed to add its value to self.user_input = tk.StringVar()
which will be printed in label. Clicking 5 button, 7 button and then 0 button will give output 570. I try to use lambda to create command for each button, but instead of getting different values I have 9 everywhere. Here is my code:
import tkinter as tk
import tkinter.ttk as ttk
class Gui(tk.Tk):
def __init__(self):
super().__init__()
self.user_input = tk.StringVar()
tk.Label(self, textvariable=self.user_input).grid()
self.create_buttons()
def create_buttons(self):
for x in range(10):
ttk.Button(self, text=x, command=lambda: self.user_input.set(self.user_input.get() + str(x))).grid()
app = Gui()
app.mainloop()
How can I fix code above to make it work as expected (description up)?