Calculator with tkinter

DragonMastur 0 Tallied Votes 691 Views Share

I needed a calculator and I didn't want to use google since my internet is slow. So I made one! Here it is.

Working with it is simple. Use the number and operation keys("c" for clear, "p" for pi, and "s" for sqrt) as well as the return and enter key, for equals, or just use the button gui! Hope you like it!

The only thing I would really like to work on is the gui. If some one would help me on that that would be greatly appreciated. I will be adding more operations, fixing the power button, and would eventually like to turn it in to a graphing calculator.

Thank you for all your support!

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

keyNames = ["1","2","3","4","5","6","7","8","9","0",".","/","*","+","-","\r","\x08"]

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 str(amount).lower() != "all":
			self.display.config(state="normal")
			self.display.delete(amount, END)
			self.display.config(state="disabled")
		elif str(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 typePi(self):
		self.delete("ALL")
		self.type(str(math.pi))

	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):
		self.delete((len(self.display.get())-1))
		if len(self.display.get()) < 1:
			self.type("0")
	
	def clear(self):
		self.addStack = []
		self.operationStack = []
		self.delete("ALL")
		self.type("0")

	def equals(self):
		try:
			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]))
		except:
			print("\nError:\n\tMust press operation to continue on equals!\n")
	
	def keyPressed(self, event):
		if repr(event.char) == "'1'":
			self.type1()
		if repr(event.char) == "'2'":
			self.type2()
		if repr(event.char) == "'3'":
			self.type3()
		if repr(event.char) == "'4'":
			self.type4()
		if repr(event.char) == "'5'":
			self.type5()
		if repr(event.char) == "'6'":
			self.type6()
		if repr(event.char) == "'7'":
			self.type7()
		if repr(event.char) == "'8'":
			self.type8()
		if repr(event.char) == "'9'":
			self.type9()
		if repr(event.char) == "'0'":
			self.type0()
		if repr(event.char) == "'.'":
			self.typeDecimal()
		if repr(event.char) == "'*'":
			self.typeTimes()
		if repr(event.char) == "'/'":
			self.typeDivide()
		if repr(event.char) == "'-'":
			self.typeMinus()
		if repr(event.char) == "'+'":
			self.typePlus()
		if repr(event.char).lower() == "'c'":
			self.clear()
		if repr(event.char) == "'\\x08'":
			self.typeDel()
		if repr(event.char) == "'\\r'":
			self.equals()
		if repr(event.char) == "'^'":
			self.typePower()
		if repr(event.char).lower() == "'s'":
			self.typeSqrt()
		if repr(event.char).lower() == "'p'":
			self.typePi()
		print("Pressed", str(repr(event.char)))

	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.root.bind("<Key>", self.keyPressed)

		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="Clear (c)", command=self.clear, width=28)
		self._sqrtbutton = Button(self.root, text="Sqrt (s)", command=self.typeSqrt, width=13)
		self._powerbutton = Button(self.root, text="^", command=self.typePower, width=13)
		self._pibutton = Button(self.root, text="Pi (p)", command=self.typePi, 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=7, column=39, rowspan=1, columnspan=13)
		self._delbutton.grid(row=7, column=26, rowspan=1, columnspan=13)
		self._clbutton.grid(row=7, 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._pibutton.grid(row=5, column=0, 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()
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.