## Sample Code of Calculator in Python

``````from tkinter import *

#create window
window = Tk()
window.title("Calculator")

#create frame
frame = Frame(window)
frame.pack()

#create buttons
buttons = []

for i in range(0,10):
buttons.append(Button(frame, text=str(i), command=lambda x=i: equation.insert(END,x)))
buttons[i].pack(side=LEFT)

# create the operator buttons
add = Button(frame, text="+", command=lambda: equation.insert(END, "+"))
sub = Button(frame, text="-", command=lambda: equation.insert(END, "-"))
mul = Button(frame, text="*", command=lambda: equation.insert(END, "*"))
div = Button(frame, text="/", command=lambda: equation.insert(END, "/"))

sub.pack(side=LEFT)
mul.pack(side=LEFT)
div.pack(side=LEFT)

#create clear entry button
clear_entry = Button(frame, text="CE", command=lambda: equation.delete(0,END))
clear_entry.pack(side=LEFT)

#create clear button
clear = Button(frame, text="C", command=lambda: equation.delete(0,END))
clear.pack(side=LEFT)

#create equal button
equal = Button(frame, text="=", command=lambda: evaluate(equation.get()))
equal.pack(side=LEFT)

#create entry field
equation = Entry(window)
equation.pack()

#function to evaluate expression
def evaluate(expression):
try:
result = eval(expression)
equation.delete(0,END)
equation.insert(0,result)
except:
equation.delete(0,END)
equation.insert(0,"Error")

#main loop
window.mainloop()

#END``````