from tkinter import *
Revised Code for Calculator
from tkinter import *
root=Tk()
root.title("Simple Calculator")
root.configure(background="Light Grey")
root.geometry('575x378')
root.resizable(width=FALSE,height=FALSE)
e=Entry(root,width=60,borderwidth=10)
e.grid(row=0,column=0,columnspan=2)
Onstatus=False
def click(number):
if(Onstatus==True):
e.insert(len(e.get()),number)
n1=e.get()
n=float(n1)
print(n,n1)
if((n!=0 and n1[0:1]=='0'and n1[1:2]!=".") or(n1[0:1]=='0' and n1[1:2]=='0') ):
e.delete(0,1)
def Operations(opr):
global fnum
global operator
first_number=e.get()
operator=opr
try:
fnum=float(first_number)
except:
print("Exception Occurred")
e.delete(0,END)
def equal():
global fnum
global operator
second_number=e.get()
e.delete(0,END)
if(operator == "+"):
e.insert(0,fnum+float(second_number))
if (operator == "-"):
e.insert(0, fnum - float(second_number))
if (operator == "*"):
e.insert(0, fnum * float(second_number))
if (operator == "/"):
e.insert(0, fnum / float(second_number))
def Clear():
e.delete(0,END)
def ON():
global Onstatus
if(Onstatus==False):
Onstatus=True
e.insert(0,"0")
else:
Onstatus=False
e.delete(0,END)
def delete():
str=e.get()[0:len(e.get())-1]
e.delete(0,END)
e.insert(0,str)
button1=Button(root,text="ON/OFF",width=25,height=2,borderwidth=5,bg='powder blue',command=ON).grid(row=0,column=2)
button2=Button(root,text="1",width=25,height=2,borderwidth=5,command=lambda number="1":click(number)).grid(row=3,column=0)
button3=Button(root,text="2",width=25,height=2,borderwidth=5,command=lambda number="2":click(number)).grid(row=3,column=1)
button4=Button(root,text="3",width=25,height=2,borderwidth=5,command=lambda number="3":click(number)).grid(row=3,column=2)
button5=Button(root,text="4",width=25,height=2,borderwidth=5,command=lambda number="4":click(number)).grid(row=2,column=0)
button6=Button(root,text="5",width=25,height=2,borderwidth=5,command=lambda number="5":click(number)).grid(row=2,column=1)
button7=Button(root,text="6",width=25,height=2,borderwidth=5,command=lambda number="6":click(number)).grid(row=2,column=2)
button8=Button(root,text="7",width=25,height=2,borderwidth=5,command=lambda number="7":click(number)).grid(row=1,column=0)
button9=Button(root,text="8",width=25,height=2,borderwidth=5,command=lambda number="8":click(number)).grid(row=1,column=1)
button10=Button(root,text="9",width=25,height=2,borderwidth=5,command=lambda number="9":click(number)).grid(row=1,column=2)
button11=Button(root,text="0",width=25,height=2,borderwidth=5,command=lambda number="0":click(number)).grid(row=4,column=0)
button12=Button(root,text="=",width=52,height=2,borderwidth=5,command=equal).grid(row=4,column=1,columnspan=2)
button13=Button(root,text="+",width=25,height=2,borderwidth=5,command=lambda opr="+":Operations(opr)).grid(row=5,column=0)
button14=Button(root,text="Clr",width=52,height=2,borderwidth=5,command=Clear).grid(row=5,column=1,columnspan=2)
button15=Button(root,text="-",width=25,height=2,borderwidth=5,command=lambda opr="-":Operations(opr)).grid(row=6,column=0)
button16=Button(root,text="*",width=25,height=2,borderwidth=5,command=lambda opr="*":Operations(opr)).grid(row=6,column=1)
button17=Button(root,text="/",width=25,height=2,borderwidth=5,command=lambda opr="/":Operations(opr)).grid(row=6,column=2)
button18=Button(root,text=".",width=25,height=2,borderwidth=5,command=lambda number=".":click(number)).grid(row=7,column=0)
button19=Button(root,text="<<--",width=52,height=2,borderwidth=5,command=delete).grid(row=7,column=1,columnspan=2)
root.mainloop()