import re
from tkinter import *
from math import sqrt
class Calc():
    s = ''
    def add_it(n):
        screen.insert(END, n)
        Calc.s+=n
    def bcksp():
        screen.delete(len(Calc.s)-1, END)
        Calc.s = Calc.s[:len(Calc.s)-1]
    def ssqrt():
        try:
            screen.delete(0,END)
            sq = round(sqrt(float(Calc.s)),3)
            if re.match(r'\d+\.{1}0{1}\b',str(sq)):
                sq = re.sub(r'(\d+)(\.0)',r'\1', str(sq))
            screen.insert(END,sq)
            Calc.s = str(float(screen.get()))
        except:
            Calc.s = ''
            screen.insert(END, ' ERROR ')
    def equ():
        try:
            x = float(re.findall(r'\-*\d+\.*\d*',Calc.s)[0])
            y = float(re.findall(r'\d+\.*\d*',Calc.s)[1])
            act = re.search(r'[\^+*/-]{1}', Calc.s[1:]).group()
            if act == '+':
                z = round(x+y,2)
            elif act == '-':
                z = round(x-y,2)
            elif act == '*':
                z = round(x*y,2)
            elif act == '^':
                z = round(x**y,2)
            else:
                z = round(x/y,2)
            if re.match(r'\d+\.{1}0{1}\b',str(z)):
                z = re.sub(r'(\d+)(\.0)',r'\1', str(z))
            screen.delete(0,END)
            screen.insert(END,z)
            Calc.s = str(z)
        except:
            Calc.s = ''
            screen.insert(END, ' ERROR ')
    def clear():
        screen.delete(0,END)
        Calc.s=''

window = Tk()
window.resizable(0,0)
window.geometry('272x308')
window.title('Simple Calculator')
screen = Entry(window, bd=8, font = 3)
one = Button(window, text='1',height=2,width=6,bd=4,bg='azure',font=14, command=lambda event='<Button-1>' : Calc.add_it('1'))
two = Button(window, text='2',height=2,width=6,bd=4,bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('2'))
three = Button(window, text='3',height=2,width=6,bd=4,bg='azure',font=14, command=lambda event='<Button-1>' : Calc.add_it('3'))
four = Button(window, text='4',height=2,width=6,bd=4, bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('4'))
five = Button(window, text='5',height=2,width=6, bd=4,bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('5'))
six = Button(window, text='6',height=2,width=6, bd=4,bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('6'))
seven = Button(window, text='7',height=2,width=6,bd=4,bg='azure',font=14, command=lambda event='<Button-1>' : Calc.add_it('7'))
eight = Button(window, text='8',height=2,width=6,bd=4,bg='azure',font=14, command=lambda event='<Button-1>' : Calc.add_it('8'))
nine = Button(window, text='9',height=2,width=6, bd=4,bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('9'))
zero = Button(window, text='0',height=2,width=6,bd=4, bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('0'))
plus = Button(window, text='+',height=2,width=6,bd=4,bg='azure',font=14, command=lambda event='<Button-1>' : Calc.add_it('+'))
minus = Button(window, text='-',height=2,width=6,bd=4,bg='azure',font=14, command=lambda event='<Button-1>' : Calc.add_it('-'))
div = Button(window, text='/',height=2,width=6,bd=4, bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('/'))
mult = Button(window, text='*',height=2,width=6, bd=4,bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('*'))
equ = Button(window, text='=',height=2,width=6,bd=4, bg='azure',font=14,command=Calc.equ)
clear = Button(window, text='C',height=2,width=6,bd=4, bg='azure',font=14,fg='red',command=Calc.clear)
bs = Button(window, text='<<',height=2,width=6,bd=4, bg='azure',font=14,fg='green', command= Calc.bcksp)
dot = Button(window, text='.',height=2,width=6,bd=4, bg='azure',font=14,fg='orange',command=lambda event='<Button-1>':Calc.add_it('.'))
sqr = Button(window, text='sq',height=2,width=6,bd=4, bg='azure',font=14,fg='orange',command=Calc.ssqrt)
exp = Button(window, text='^',height=2,width=6, bd=4,bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('^'))

screen.grid(row=0,rowspan=2,columnspan=12)
exp.grid(row=2,column=0)
sqr.grid(row=2,column=1)
bs.grid(row=2,column=2)
clear.grid(row=2,column=3)
plus.grid(row=3,column=0)
minus.grid(row=3,column=1)
mult.grid(row=3,column=2)
div.grid(row=3,column=3)
one.grid(row=4,column=0)
two.grid(row=4,column=1)
three.grid(row=4,column=2)
four.grid(row=4,column=3)
five.grid(row=5,column=0)
six.grid(row=5,column=1)
seven.grid(row=5,column=2)
eight.grid(row=5,column=3)
nine.grid(row=6,column=0)
zero.grid(row=6,column=1)
dot.grid(row=6,column=2)
equ.grid(row=6,column=3)

window.mainloop()
