You are on page 1of 3

from tkinter import Tk, Label, StringVar, Button, Frame

def button(num):
global equation_txt

if num == "del":
equation_txt = equation_txt[:-1]
else:
equation_txt += str(num)

equationLabel.set(equation_txt)
label.config(fg="white")

def equals():
global equation_txt

try:
equation_txt = equation_txt.replace('%', '/100')

total = str(eval(equation_txt))
equationLabel.set(total)
equation_txt = total

except SyntaxError:
equationLabel.set("Syntax Error!")
equation_txt = ""

except ZeroDivisionError:
equationLabel.set("Error!")
equation_txt = ""

def clear():
global equation_txt

equationLabel.set("")
equation_txt = ""

window = Tk()
window.title("Calculator Group6")
window.geometry("570x600+100+200")
equation_txt = ""
equationLabel = StringVar()

label = Label(window, textvariable=equationLabel, font=('serif', 20), bg="black",


fg="white", width=22, height=2)
label.pack()

frame = Frame()
frame.pack()

buttonC = Button(frame, text="C", height=1, width=5, font=('serif', 20),


bg="#FF7D33", fg="black", command=clear)
buttonC.grid(row=0, column=0)

percent = Button(frame, text="%", height=1, width=5, font=('serif', 20),


bg="#FF7D33", fg="black", command=lambda: button('%'))
percent.grid(row=0, column=1)

delete = Button(frame, text="del", height=1, width=5, font=('serif', 20),


bg="#FF7D33", fg="black", command=lambda: button('del'))
delete.grid(row=0, column=2)

button7 = Button(frame, text=" 7", height=1, width=5, font=('serif', 20),


fg="black", command=lambda: button(7))
button7.grid(row=1, column=0)

button8 = Button(frame, text=" 8",height=1, width=5, font=('serif', 20),


fg="black", command=lambda: button(8))
button8.grid(row=1, column=1)

button9 = Button(frame, text=" 9",height=1, width=5, font=('serif', 20),


fg="black", command=lambda: button(9))
button9.grid(row=1, column=2)

button4 = Button(frame, text=" 4", height=1, width=5, font=('serif', 20),


fg="black", command=lambda: button(4))
button4.grid(row=2, column=0)

button5 = Button(frame, text=" 5", height=1, width=5, font=('serif', 20),


fg="black", command=lambda: button(5))
button5.grid(row=2, column=1)

button6 = Button(frame, text=" 6", height=1, width=5, font=('serif', 20),


fg="black", command=lambda: button(6))
button6.grid(row=2, column=2)

button1 = Button(frame, text=" 1", height=1, width=5, font=('serif', 20),


fg="black", command=lambda: button(1))
button1.grid(row=3, column=0)

button2 = Button(frame, text=" 2", height=1, width=5, font=('serif', 20),


fg="black", command=lambda: button(2))
button2.grid(row=3, column=1)

button3 = Button(frame, text=" 3", height=1, width=5, font=('serif', 20),


fg="black", command=lambda: button(3))
button3.grid(row=3, column=2)

button00 = Button(frame, text="00", height=1, width=5, font=('serif', 20),


fg="black", command=lambda: button('00'))
button00.grid(row=4, column=0)

button0 = Button(frame, text="0", height=1, width=5, font=('serif', 20),


fg="black", command=lambda: button(0))
button0.grid(row=4, column=1)

add = Button(frame, text="+", height=1, width=5, font=('serif', 20), fg="black",


command=lambda: button('+'))
add.grid(row=3, column=3)

minus = Button(frame, text="-", height=1, width=5, font=('serif', 20), fg="black",


command=lambda: button('-'))
minus.grid(row=2, column=3)

multiply = Button(frame, text="*", height=1, width=5, font=('serif', 20),


fg="black", command=lambda: button('*'))
multiply.grid(row=1, column=3)
divide = Button(frame, text="/", height=1, width=5, font=('serif', 20), fg="black",
command=lambda: button('/'))
divide.grid(row=0, column=3)

equal = Button(frame, text="=", height=1, width=5, font=('serif', 20), fg="black",


command=equals)
equal.grid(row=4, column=3)

decimal = Button(frame, text=".", height=1, width=5, font=('serif', 20),


fg="black", command=lambda: button('.'))
decimal.grid(row=4, column=2)

window.mainloop()

You might also like