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 ( )
aW1wb3J0IHJlCmZyb20gdGtpbnRlciBpbXBvcnQgKgpmcm9tIG1hdGggaW1wb3J0IHNxcnQKY2xhc3MgQ2FsYygpOgogICAgcyA9ICcnCiAgICBkZWYgYWRkX2l0KG4pOgogICAgICAgIHNjcmVlbi5pbnNlcnQoRU5ELCBuKQogICAgICAgIENhbGMucys9bgogICAgZGVmIGJja3NwKCk6CiAgICAgICAgc2NyZWVuLmRlbGV0ZShsZW4oQ2FsYy5zKS0xLCBFTkQpCiAgICAgICAgQ2FsYy5zID0gQ2FsYy5zWzpsZW4oQ2FsYy5zKS0xXQogICAgZGVmIHNzcXJ0KCk6CiAgICAgICAgdHJ5OgogICAgICAgICAgICBzY3JlZW4uZGVsZXRlKDAsRU5EKQogICAgICAgICAgICBzcSA9IHJvdW5kKHNxcnQoZmxvYXQoQ2FsYy5zKSksMykKICAgICAgICAgICAgaWYgcmUubWF0Y2gocidcZCtcLnsxfTB7MX1cYicsc3RyKHNxKSk6CiAgICAgICAgICAgICAgICBzcSA9IHJlLnN1YihyJyhcZCspKFwuMCknLHInXDEnLCBzdHIoc3EpKQogICAgICAgICAgICBzY3JlZW4uaW5zZXJ0KEVORCxzcSkKICAgICAgICAgICAgQ2FsYy5zID0gc3RyKGZsb2F0KHNjcmVlbi5nZXQoKSkpCiAgICAgICAgZXhjZXB0OgogICAgICAgICAgICBDYWxjLnMgPSAnJwogICAgICAgICAgICBzY3JlZW4uaW5zZXJ0KEVORCwgJyBFUlJPUiAnKQogICAgZGVmIGVxdSgpOgogICAgICAgIHRyeToKICAgICAgICAgICAgeCA9IGZsb2F0KHJlLmZpbmRhbGwocidcLSpcZCtcLipcZConLENhbGMucylbMF0pCiAgICAgICAgICAgIHkgPSBmbG9hdChyZS5maW5kYWxsKHInXGQrXC4qXGQqJyxDYWxjLnMpWzFdKQogICAgICAgICAgICBhY3QgPSByZS5zZWFyY2gocidbXF4rKi8tXXsxfScsIENhbGMuc1sxOl0pLmdyb3VwKCkKICAgICAgICAgICAgaWYgYWN0ID09ICcrJzoKICAgICAgICAgICAgICAgIHogPSByb3VuZCh4K3ksMikKICAgICAgICAgICAgZWxpZiBhY3QgPT0gJy0nOgogICAgICAgICAgICAgICAgeiA9IHJvdW5kKHgteSwyKQogICAgICAgICAgICBlbGlmIGFjdCA9PSAnKic6CiAgICAgICAgICAgICAgICB6ID0gcm91bmQoeCp5LDIpCiAgICAgICAgICAgIGVsaWYgYWN0ID09ICdeJzoKICAgICAgICAgICAgICAgIHogPSByb3VuZCh4Kip5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