I've written a simple calculator code in Python using Tkinter. Almost every function works but I can't seem to figure out what the lambda function does in my project. If I add it works without any bug but if I remove it shows an error.
Here is the block where the error points out
class Calc:
def __init__(self):
self.total = 0
self.current = ''
self.ip_val = True
self.check_sum = False
self.op = ''
self.result = False
def operation(self, op):
self.current = float(self.current) #this line generates error
if self.check_sum:
self.valid_function()
elif not self.result:
self.total = self.current
self.ip_val = True
self.check_sum = True
self.op = op
self.result = False
Here's the line calling the CALC class's operation method.
Button(calc, text='x^y', width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="gray20",
command= res.operation('pow')).grid(row=1, column=5, pady=1)
ERROR MESSAGE
Traceback (most recent call last):
File "T:/WorkSpace/PythonCourse/Day-6.py", line 235, in <module>
command= res.operation('pow')).grid(row=1, column=5, pady=1)
File "T:/WorkSpace/PythonCourse/Day-6.py", line 69, in operation
self.current = float(self.current)
ValueError: could not convert string to float: ''
FYI, I know IF I initialize the Button class like below ( with lambda function )
Button(calc, text='x^y', width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="gray20",
command= lambda: res.operation('pow')).grid(row=1, column=5, pady=1)
IT WORKS JUST FINE.
QUERY
I want an explanation of how this lambda function is the solution to this error. And also if the user doesn't press the button, the function shouldn't call itself. How's this even compiling?