Mar-11-2018, 06:14 PM
(This post was last modified: Mar-11-2018, 06:14 PM by TheRedFedora.)
Hello everyone,
I had the plan to make a calculator for my schoolproject. Everything I did seemed fine until the point that I actually ran the whole thing. Then the error occurred.
*Quick edit, the GUI is working, the error occurres after I put in my first variable and press on any of the options from equate. Then it doesn't type numbers anymore and gives an error when I hit the "="
TheRedFedora
I had the plan to make a calculator for my schoolproject. Everything I did seemed fine until the point that I actually ran the whole thing. Then the error occurred.
Error:line 33, in equate
self.result = float(self.var1) - float(self.var2)
ValueError: could not convert string to float: this is my code: from tkinter import *
master = Tk()
display = Entry(master, width=20, justify='right', bd=0, bg='lightgrey')
master.title("calculator")
class Calculator:
def __init__ (self):
self.var1 = ""
self.var2 = ""
self.result = 0
self.current = 0
self.operator = 0
def number_button(self, index):
if self.current is 0:
self.var1 = str(self.var1) + str(index)
display.delete(0, END)
display.insert(0,string=self.var1)
else:
self.var1 = str(self.var2) + str(index)
display.delete(0, END)
display.insert(0, string=self.var2)
def equate(self):
if self.operator is 0:
self.result = float(self.var1) + float(self.var2)
elif self.operator is 1:
self.result = float(self.var1) - float(self.var2)
elif self.operator is 2:
self.result = float(self.var1) * float(self.var2)
elif self.operator is 3:
self.result = float(self.var1) / float(self.var2)
display.delete(0, END)
display.insert(0, string=self.result)
def set_op(self, op):
self.operator = op
display.delete(0, END)
if self.current is 0:
self.current = 1
else:
self.equate()
self.var2=""
def clear(self):
self.__init__()
display.delete(0, END)
calc = Calculator()
b0 = Button(master, text="0", command=lambda: calc.number_button(0))
b1 = Button(master, text="1", command=lambda: calc.number_button(1))
b2 = Button(master, text="2", command=lambda: calc.number_button(2))
b3 = Button(master, text="3", command=lambda: calc.number_button(3))
b4 = Button(master, text="4", command=lambda: calc.number_button(4))
b5 = Button(master, text="5", command=lambda: calc.number_button(5))
b6 = Button(master, text="6", command=lambda: calc.number_button(6))
b7 = Button(master, text="7", command=lambda: calc.number_button(7))
b8 = Button(master, text="8", command=lambda: calc.number_button(8))
b9 = Button(master, text="9", command=lambda: calc.number_button(9))
b_dot = Button(master, text=".", command=lambda: calc.number_button("."))
plus = Button(master, text="+", command=lambda: calc.set_op(0))
minus = Button(master, text="-", command=lambda: calc.set_op(1))
times = Button(master, text="x", command=lambda: calc.set_op(2))
divide = Button(master, text="/", command=lambda: calc.set_op(3))
equals = Button (master, text="=", command=calc.equate)
clear = Button (master, text="c", command=calc.clear)
display.place(x=0, y=2)
b7.grid(row=1, column=0)
b8.grid(row=1, column=1)
b9.grid(row=1, column=2)
b4.grid(row=2, column=0)
b5.grid(row=2, column=1)
b6.grid(row=2, column=2)
b1.grid(row=3, column=0)
b2.grid(row=3, column=1)
b3.grid(row=3, column=2)
b0.grid(row=4, column=0)
b_dot.grid(row=4, column=1)
clear.grid(row=4, column=2)
plus.grid(row=1, column=3)
minus.grid(row=2, column=3)
times.grid(row=3, column=3)
divide.grid(row=4, column=3)
equals.grid(row=4, column=3)
master.mainloop()The problem seems to be coming out of the "equate" section. Is there a way to fix it or is there maybe an alternative code that would do the trick?*Quick edit, the GUI is working, the error occurres after I put in my first variable and press on any of the options from equate. Then it doesn't type numbers anymore and gives an error when I hit the "="
TheRedFedora
