# Python GUI Calculator
#Credit : Subhash Bhandari
from tkinter import *
root = Tk()
root.title("Calculator")
root.iconbitmap('images/calculator.ico')
def clicked(num):
current = e.get()
e.delete(0,END)
e.insert(0,current + str(num))
def addition():
first_num = e.get()
global f_num
global math
math = "addition"
f_num = int(first_num)
e.delete(0,END)
def substraction():
first_num = e.get()
global f_num
global math
math = "substraction"
f_num = int(first_num)
e.delete(0,END)
try:
def multiplication():
first_num = e.get()
global f_num
global math
math = "multiplication"
f_num = int(first_num)
e.delete(0,END)
except ValueError:
e.insert(root,text="Error!")
def division():
first_num = e.get()
global f_num
global math
math = "division"
f_num = int(first_num)
e.delete(0,END)
def clear():
e.delete(0,END)
def equals():
second_num = e.get()
e.delete(0,END)
if math == "addition":
e.insert(0,f_num + int(second_num))
elif math == "substraction":
e.insert(0,f_num - int(second_num))
elif math == "division":
e.insert(0,f_num / int(second_num))
elif math == "multiplication":
e.insert(0,f_num * int(second_num))
e = Entry(root,width=50,borderwidth = 4,fg="white",bg="purple")
e.grid(row=0,columnspan = 5,pady=5)
x = 30
y = 30
one = Button(root, text="1",padx=x,pady=y,bg="brown",fg="white",command=lambda: clicked(1))
two = Button(root, text="2",padx=x,pady=y,bg="brown",fg="white",command=lambda:clicked(2))
three = Button(root, text="3",padx=x,pady=y,bg="brown",fg="white",command=lambda:clicked(3))
four = Button(root, text="4",padx=x,pady=y,bg="brown",fg="white",command=lambda:clicked(4))
five = Button(root, text="5",padx=x,pady=y,bg="brown",fg="white",command=lambda:clicked(5))
six = Button(root, text="6",padx=x,pady=y,bg="brown",fg="white",command=lambda:clicked(6))
seven = Button(root, text="7",padx=x,pady=y,bg="brown",fg="white",command=lambda:clicked(7))
eight = Button(root, text="8",padx=x,pady=y,bg="brown",fg="white",command=lambda:clicked(8))
nine = Button(root, text="9",padx=x,pady=y,bg="brown",fg="white",command=lambda:clicked(9))
zero = Button(root, text="0",padx=x,pady=y,bg="brown",fg="white",command=lambda:clicked(0))
add = Button(root, text="+",padx=x,pady=y,bg="orange",fg="white",command=addition)
substract = Button(root, text="-",padx=x,pady=y,bg="orange",fg="white",command=substraction)
divide = Button(root, text="/",padx=x,pady=y,bg="orange",fg="white",command=division)
multiply = Button(root, text="*",padx=x,pady=y,bg="orange",fg="white",command=multiplication)
equal = Button(root, text="=",padx=x,pady=y,bg="blue",fg="white",command=equals)
clear = Button(root,text="C",padx=x , pady=y,bg="red",fg="white",command = clear)
#placing buttons
one.grid(row=3,column=0)
two.grid(row=3,column=1)
three.grid(row=3,column=2)
four.grid(row=2,column=0)
five.grid(row=2,column=1)
six.grid(row=2,column=2)
seven.grid(row=1,column=0)
eight.grid(row=1,column=1)
nine.grid(row=1,column=2)
zero.grid(row=4,column=0)
add.grid(row=4,column=1)
substract.grid(row=4,column=2)
divide.grid(row=2,column=4)
multiply.grid(row=3,column=4)
equal.grid(row=4,column=4)
clear.grid(row=1,column=4)
root.mainloop()