How can I create 0-9 number buttons with lambda using tkinter?
Put the bulk of your code in a proper function. A best practice is that a lambda for a widget callback should only ever call a single function. Complex lambdas are difficult to read and difficult to debug.
The second part of the solution is to create a closure. The trick for that is to make the variable you're passing in bound to the lambda as a default argument.
The callback looks something like this:
def callback(self, number): new_value = self.user_input.get() + str(number) self.user_input.set(new_value)
Defining each button looks something like this:
def create_buttons(self): for x in range(10): button = ttk.Button(self, text=x, command=lambda number=x: self.callback(number)) button.grid()
Pay particular attention to number=x
as part of the lambda definition. This is where the current value of x is bound to the number
parameter inside the lambda.