Why do i have to put button commands as lambda functions?

Viewed 22

I was watching this tutorial on how to create a GUI calculator. I don't understand why he puts button commands as lambda functions. Aren't they already functions?

button3 = Button(frame, text=3, height=4, width=9, font=35,
                 command= lambda: button_press(3))
button3.grid(row=0, column=2)
1 Answers

Why do i have to put button commands as lambda functions?

You don't have to use lambda. If you don't need to pass arguments you can use a reference to a function that takes no parameters. However, if you want to pass arguments then you'll have to do something that allows you to use parameters. lambda isn't the only solution, it is just one of the most convenient.

You could also use functools.partial or some other technique such as a unique function for every button.

Related