Calculator

DragonMastur 0 Tallied Votes 530 Views Share

It could be simplifide more, I know. I was doing it as an exersise so its rough and skecthy. But it's workable, I tested it more then 50 times.

# coding: iso-8859-15
from tkinter import *
import math

class Calculator:
	def type(self, str):
		self.display.config(state="normal")
		self.display.insert(END, str)
		self.display.config(state="disabled")
	def delete(self, amount):
		if amount.lower() != "all":
			self.display.config(state="normal")
			self.display.delete(amount, END)
			self.display.config(state="disabled")
		elif amount.lower() == "all":
			self.display.config(state="normal")
			self.display.delete(0, END)
			self.display.config(state="disabled")

	def typeNumber(self, number):
		if len(self.display.get()) != 60:
			if self.display.get().startswith("0") == True and self.display.get().startswith("0.") != True:
				self.delete("0")
				self.type(number)
			else:
				self.type(number)

	def type1(self):
		self.typeNumber("1")
	def type2(self):
		self.typeNumber("2")
	def type3(self):
		self.typeNumber("3")
	def type4(self):
		self.typeNumber("4")
	def type5(self):
		self.typeNumber("5")
	def type6(self):
		self.typeNumber("6")
	def type7(self):
		self.typeNumber("7")
	def type8(self):
		self.typeNumber("8")
	def type9(self):
		self.typeNumber("9")
	def type0(self):
		self.typeNumber("0")

	def typePlus(self):
		self.addStack = []
		self.addStack.append(self.display.get())
		self.operationStack = []
		self.operationStack.append("+")
		self.delete("ALL")
		self.type("0")
	def typeMinus(self):
		self.addStack = []
		self.addStack.append(self.display.get())
		self.operationStack = []
		self.operationStack.append("-")
		self.delete("ALL")
		self.type("0")
	def typeTimes(self):
		self.addStack = []
		self.addStack.append(self.display.get())
		self.operationStack = []
		self.operationStack.append("*")
		self.delete("ALL")
		self.type("0")
	def typeDivide(self):
		self.addStack = []
		self.addStack.append(self.display.get())
		self.operationStack = []
		self.operationStack.append("/")
		self.delete("ALL")
		self.type("0")
	def typeSqrt(self):
		self.addStack = []
		self.operationStack = []
		self.addStack.append(self.display.get())
		self.addStack.append(math.sqrt(float(self.addStack[0])))
		del self.addStack[0]
		self.delete("ALL")
		if str(self.addStack[0]).endswith(".0") == True:
			self.type(str(int(self.addStack[0])))
		else:
			self.type(str(self.addStack[0]))
	def typePower(self):
		self.addStack = []
		self.operationStack = []
		self.operationStack.append("**")
		self.delete("ALL")
		self.type("0")
	def typeDecimal(self):
		if "." not in self.display.get():
			self.type(".")
		else:
			print(self.diaplay.get())
	def typeDel(self):
		pass
	
	def clear(self):
		self.addStack = []
		self.operationStack = []
		self.delete("ALL")
		self.type("0")

	def equals(self):
		if self.operationStack[0] == "+":
			self.addStack.append(self.display.get())
			self.addStack.append(float(self.addStack[0]) + float(self.display.get()))
			del self.addStack[0]
			del self.addStack[0]
		elif self.operationStack[0] == "-":
			self.addStack.append(self.display.get())
			self.addStack.append(float(self.addStack[0]) - float(self.display.get()))
			del self.addStack[0]
			del self.addStack[0]
		elif self.operationStack[0] == "*":
			self.addStack.append(self.display.get())
			self.addStack.append(float(self.addStack[0]) * float(self.display.get()))
			del self.addStack[0]
			del self.addStack[0]
		elif self.operationStack[0] == "/":
			self.addStack.append(self.display.get())
			self.addStack.append(float(self.addStack[0]) / float(self.display.get()))
			del self.addStack[0]
			del self.addStack[0]
		elif self.operationStack[0] == "**":
			self.addStack.append(self.display.get())
			self.addStack.append(float(self.addStack[0]) ** float(self.display.get()))
			del self.addStack[0]
		self.delete("ALL")
		if str(self.addStack[0]).endswith(".0") == True:
			self.type(str(int(self.addStack[0])))
		else:
			self.type(str(self.addStack[0]))

	def __init__(self):
		self.addStack = []
		self.operationStack = []

		self.root = Tk()
		self.root.geometry("363x250")
		#self.root.minsize(width=400, height=700)
		#self.root.maxsize(width=400, height=700)
		self.root.title("Calculator")

		self.display = Entry(self.root, width=60)
		self.display.insert(END, "0")
		self.display.config(state="disabled")
		self.displayLabel = Label(self.root, text="")

		self._plusbutton = Button(self.root, text="+", command=self.typePlus, width=13)
		self._minusbutton = Button(self.root, text="-", command=self.typeMinus, width=13)
		self._timesbutton = Button(self.root, text="x", command=self.typeTimes, width=13)
		self._dividebutton = Button(self.root, text="÷", command=self.typeDivide, width=13)
		self._decimalbutton = Button(self.root, text=".", command=self.typeDecimal, width=13)
		self._equalsbutton = Button(self.root, text="=", command=self.equals, width=13)
		self._delbutton = Button(self.root, text="Del", command=self.typeDel, width=13)
		self._clbutton = Button(self.root, text="C", command=self.clear, width=28)
		self._sqrtbutton = Button(self.root, text="Sqrt", command=self.typeSqrt, width=13)
		self._powerbutton = Button(self.root, text="^", command=self.typePower, width=13)
		self._1button = Button(self.root, text="1", command=self.type1, width=13)
		self._2button = Button(self.root, text="2", command=self.type2, width=13)
		self._3button = Button(self.root, text="3", command=self.type3, width=13)
		self._4button = Button(self.root, text="4", command=self.type4, width=13)
		self._5button = Button(self.root, text="5", command=self.type5, width=13)
		self._6button = Button(self.root, text="6", command=self.type6, width=13)
		self._7button = Button(self.root, text="7", command=self.type7, width=13)
		self._8button = Button(self.root, text="8", command=self.type8, width=13)
		self._9button = Button(self.root, text="9", command=self.type9, width=13)
		self._0button = Button(self.root, text="0", command=self.type0, width=13)

		self.displayLabel.grid(row=0,column=0,rowspan=1,columnspan=52)
		self.display.grid(row=1,column=0, rowspan=1, columnspan=52)
		self._plusbutton.grid(row=2, column=39, rowspan=1, columnspan=13)
		self._minusbutton.grid(row=3, column=39, rowspan=1, columnspan=13)
		self._timesbutton.grid(row=4, column=39, rowspan=1, columnspan=13)
		self._dividebutton.grid(row=5, column=39, rowspan=1, columnspan=13)
		self._decimalbutton.grid(row=5, column=26, rowspan=1, columnspan=13)
		self._equalsbutton.grid(row=5, column=0, rowspan=1, columnspan=13)
		self._clbutton.grid(row=6, column=0, rowspan=1, columnspan=26)
		self._sqrtbutton.grid(row=6, column=26, rowspan=1, columnspan=13)
		self._powerbutton.grid(row=6, column=39, rowspan=1, columnspan=13)
		self._1button.grid(row=2, column=0, rowspan=1, columnspan=13)
		self._2button.grid(row=2, column=13, rowspan=1, columnspan=13)
		self._3button.grid(row=2, column=26, rowspan=1, columnspan=13)
		self._4button.grid(row=3, column=0, rowspan=1, columnspan=13)
		self._5button.grid(row=3, column=13, rowspan=1, columnspan=13)
		self._6button.grid(row=3, column=26, rowspan=1, columnspan=13)
		self._7button.grid(row=4, column=0, rowspan=1, columnspan=13)
		self._8button.grid(row=4, column=13, rowspan=1, columnspan=13)
		self._9button.grid(row=4, column=26, rowspan=1, columnspan=13)
		self._0button.grid(row=5, column=13, rowspan=1, columnspan=13)

if __name__ == "__main__":
	app = Calculator()
	app.root.mainloop()
Gribouillis 1,391 Programming Explorer Team Colleague

The arithmetic operations don't work for me. Only the sqrt works.

fonzali 0 Light Poster

it worked for me , no problem

Slass33 0 Newbie Poster

Good code, it works, but took me forever to get out the number lines when I coppied and pasted it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.