in the process of learning Python. I understand the use of lambda, here's an example and link(for others).
Why are Python lambdas useful?
mult3 = filter(lambda x: x % 3 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9])
Would assign [3, 6, 9] to mult3.
Here is the code I was required to do in the book's quiz:
import tkinter
def counter(text):
"""Add 1 every time the button is pressed"""
count = int(text.get())
text.set(count + 1)
window = tkinter.Tk()
frame = tkinter.Frame(window)
frame.pack()
var = tkinter.IntVar()
var.set(0)
button = tkinter.Button(window, textvariable=var, command=lambda: counter(var))
button.pack()
tkinter.mainloop()
That is the verified solution. What I had come up with was the same except for the code in variable button. Here's my line:
button = tkinter.Button(window, textvariable=var, command=counter(var))
The absence of lambda prevents the counter from working. I don't understand why lambda is required? As I'm just calling a function I had made, I'm not requiring a new unnamed function..?
Thanks in advance for your time :)