Python Forum
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyCalc(V1.5.1) Errors!
#1
This is a python calculator I am making. Based off of source code from: BlendyCat.

I just wanted someone with more experience than me to make sure there were no errors. And to see if any basic improvements to be made. But I do have one problem, sometimes when I run the code: a million errors come up. Other times, mostly when I run it in debug mode (in PyCharm). Are there any code mistakes. Since I changed some of the code, and have only been doing python for about a month now.

from tkinter import *


master = Tk()
display = Entry(master, width=11, justify='right', bd=0, bg='lightgrey')

master.title("PyCalc")


class Calculator:
   def __init__(self):
       self.var1 = ""
       self.var2 = ""
       self.result = 0
       self.current = 0
       self.operator = 0

   def numb_butt(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.var2 = 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.numb_butt(0))
b1 = Button(master, text="1", command=lambda: calc.numb_butt(1))
b2 = Button(master, text="2", command=lambda: calc.numb_butt(2))
b3 = Button(master, text="3", command=lambda: calc.numb_butt(3))
b4 = Button(master, text="4", command=lambda: calc.numb_butt(4))
b5 = Button(master, text="5", command=lambda: calc.numb_butt(5))
b6 = Button(master, text="6", command=lambda: calc.numb_butt(6))
b7 = Button(master, text="7", command=lambda: calc.numb_butt(7))
b8 = Button(master, text="8", command=lambda: calc.numb_butt(8))
b9 = Button(master, text="9", command=lambda: calc.numb_butt(9))
b_dot = Button(master, text="•", command=lambda: calc.numb_butt("."))

plus = Button(master, text="+", command=lambda: calc.set_op(0))
minus = Button(master, text="-", command=lambda: calc.set_op(1))
times = Button(master, text="×", 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=2, column=0)
b8.grid(row=2, column=1)
b9.grid(row=2, column=2)
b4.grid(row=3, column=0)
b5.grid(row=3, column=1)
b6.grid(row=3, column=2)
b1.grid(row=4, column=0)
b2.grid(row=4, column=1)
b3.grid(row=4, column=2)
b0.grid(row=5, column=1)

b_dot.grid(row=5, column=2)
clear.grid(row=5, column=0)

plus.grid(row=2, column=3)
minus.grid(row=3, column=3)
times.grid(row=4, column=3)
divide.grid(row=5, column=3)
equals.grid(row=1, column=4)

master.mainloop()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020