import re
from tkinter import *
class Calc( ) :
s = ''
def add_it( n) :
screen.insert ( END, n)
Calc.s += n
def equ( ) :
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 = x+y
elif act == '-' :
z = x-y
elif act == '*' :
z = x*y
else :
z = x/y
screen.delete ( 0 , END)
screen.insert ( END, z)
Calc.s = str ( z)
def clear( ) :
screen.delete ( 0 , END)
Calc.s = ''
window = Tk( )
window.resizable ( 0 , 0 )
window.geometry ( '228x220' )
window.title ( 'Simple Calculator' )
screen = Entry( window, bd= 8 , font = 3 )
one = Button( window, text= '1' , height= 2 , width= 6 , bd= 4 , bg= 'azure' , command= lambda event= '<Button-1>' : Calc.add_it ( '1' ) )
two = Button( window, text= '2' , height= 2 , width= 6 , bd= 4 , bg= 'azure' , command= lambda event= '<Button-1>' : Calc.add_it ( '2' ) )
three = Button( window, text= '3' , height= 2 , width= 6 , bd= 4 , bg= 'azure' , command= lambda event= '<Button-1>' : Calc.add_it ( '3' ) )
four = Button( window, text= '4' , height= 2 , width= 6 , bd= 4 , bg= 'azure' , command= lambda event= '<Button-1>' : Calc.add_it ( '4' ) )
five = Button( window, text= '5' , height= 2 , width= 6 , bd= 4 , bg= 'azure' , command= lambda event= '<Button-1>' : Calc.add_it ( '5' ) )
six = Button( window, text= '6' , height= 2 , width= 6 , bd= 4 , bg= 'azure' , command= lambda event= '<Button-1>' : Calc.add_it ( '6' ) )
seven = Button( window, text= '7' , height= 2 , width= 6 , bd= 4 , bg= 'azure' , command= lambda event= '<Button-1>' : Calc.add_it ( '7' ) )
eight = Button( window, text= '8' , height= 2 , width= 6 , bd= 4 , bg= 'azure' , command= lambda event= '<Button-1>' : Calc.add_it ( '8' ) )
nine = Button( window, text= '9' , height= 2 , width= 6 , bd= 4 , bg= 'azure' , command= lambda event= '<Button-1>' : Calc.add_it ( '9' ) )
zero = Button( window, text= '0' , height= 2 , width= 6 , bd= 4 , bg= 'azure' , command= lambda event= '<Button-1>' : Calc.add_it ( '0' ) )
plus = Button( window, text= '+' , height= 2 , width= 6 , bd= 4 , bg= 'azure' , command= lambda event= '<Button-1>' : Calc.add_it ( '+' ) )
minus = Button( window, text= '-' , height= 2 , width= 6 , bd= 4 , bg= 'azure' , command= lambda event= '<Button-1>' : Calc.add_it ( '-' ) )
div = Button( window, text= '/' , height= 2 , width= 6 , bd= 4 , bg= 'azure' , command= lambda event= '<Button-1>' : Calc.add_it ( '/' ) )
mult = Button( window, text= '*' , height= 2 , width= 6 , bd= 4 , bg= 'azure' , command= lambda event= '<Button-1>' : Calc.add_it ( '*' ) )
equ = Button( window, text= '=' , height= 2 , width= 6 , bd= 4 , bg= 'azure' , command= Calc.equ )
clear = Button( window, text= 'C' , height= 2 , width= 6 , bd= 4 , bg= 'azure' , fg= 'red' , command= Calc.clear )
screen.grid ( row= 0 , rowspan= 2 , columnspan= 12 )
one.grid ( row= 2 , column= 0 )
two.grid ( row= 2 , column= 1 )
three.grid ( row= 2 , column= 2 )
four.grid ( row= 2 , column= 3 )
five.grid ( row= 3 , column= 0 )
six.grid ( row= 3 , column= 1 )
seven.grid ( row= 3 , column= 2 )
eight.grid ( row= 3 , column= 3 )
nine.grid ( row= 4 , column= 0 )
zero.grid ( row= 4 , column= 1 )
plus.grid ( row= 4 , column= 2 )
minus.grid ( row= 4 , column= 3 )
mult.grid ( row= 5 , column= 0 )
div.grid ( row= 5 , column= 1 )
clear.grid ( row= 5 , column= 2 )
equ.grid ( row= 5 , column= 3 )
window.mainloop ( )
aW1wb3J0IHJlCmZyb20gdGtpbnRlciBpbXBvcnQgKgpjbGFzcyBDYWxjKCk6CiAgICBzID0gJycKICAgIGRlZiBhZGRfaXQobik6CiAgICAgICAgc2NyZWVuLmluc2VydChFTkQsIG4pCiAgICAgICAgQ2FsYy5zKz1uCiAgICBkZWYgZXF1K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