Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- calc = ""
- def press(num):
- global calc
- calc = calc + str(num)
- eq.set(calc)
- def total():
- try:
- global calc
- equal = str(eval(calc))
- eq.set(equal)
- calc = ""
- except:
- eq.set("Error!")
- calc = ""
- def clear():
- global calc
- calc = ""
- eq.set("")
- ##GUI Code
- gui = Tk()
- #Properties
- ##gui.configure(background="white")
- gui.title("Maciejs Calculator")
- gui.geometry("375x150")
- eq = StringVar()
- calc_field = Entry(gui, textvariable=eq)
- calc_field.grid(columnspan=4, ipadx=70)
- eq.set("This Calculator is Amazing")
- #Buttons
- btn1 = Button(gui, text="1", fg="black", bg="light blue", command=lambda: press(1), height=1, width=7)
- btn1.grid(row=2, column=0)
- btn2 = Button(gui, text="2", fg="black", bg="light blue", command=lambda: press(2), height=1, width=7)
- btn2.grid(row=2, column=1)
- btn3 = Button(gui, text="3", fg="black", bg="light blue", command=lambda: press(3), height=1, width=7)
- btn3.grid(row=2, column=2)
- btn4 = Button(gui, text="4", fg="black", bg="light blue", command=lambda: press(4), height=1, width=7)
- btn4.grid(row=3, column=0)
- btn5 = Button(gui, text="5", fg="black", bg="light blue", command=lambda: press(5), height=1, width=7)
- btn5.grid(row=3, column=1)
- btn6 = Button(gui, text="6", fg="black", bg="light blue", command=lambda: press(6), height=1, width=7)
- btn6.grid(row=3, column=2)
- btn7 = Button(gui, text="7", fg="black", bg="light blue", command=lambda: press(7), height=1, width=7)
- btn7.grid(row=4, column=0)
- btn8 = Button(gui, text="8", fg="black", bg="light blue", command=lambda: press(8), height=1, width=7)
- btn8.grid(row=4, column=1)
- btn9 = Button(gui, text="9", fg="black", bg="light blue", command=lambda: press(9), height=1, width=7)
- btn9.grid(row=4, column=2)
- btn0 = Button(gui, text="0", fg="black", bg="light blue", command=lambda: press(0), height=1, width=7)
- btn0.grid(row=5, column=1)
- btnClr = Button(gui, text="Clear", fg="black", bg="light green", command=lambda: clear(), height=1, width=7)
- btnClr.grid(row=5, column=0)
- btnEq = Button(gui, text="=", fg="black", bg="light green", command=lambda: total(), height=1, width=7)
- btnEq.grid(row=5, column=2)
- btnPlus = Button(gui, text="+", fg="black", bg="light green", command=lambda: press("+"), height=1, width=7)
- btnPlus.grid(row=2, column=3)
- btnMinus = Button(gui, text="-", fg="black", bg="light green", command=lambda: press("-"), height=1, width=7)
- btnMinus.grid(row=3, column=3)
- btnMult = Button(gui, text="*", fg="black", bg="light green", command=lambda: press("*"), height=1, width=7)
- btnMult.grid(row=4, column=3)
- btnDiv = Button(gui, text="/", fg="black", bg="light green", command=lambda: press("/"), height=1, width=7)
- btnDiv.grid(row=5, column=3)
- gui.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement