Making GUI Calculator in Tkinter Python
from tkinter import *
abdur = Tk()
abdur.geometry("480x600")
abdur.minsize(480, 600)
abdur.maxsize(480, 600)
abdur.wm_iconbitmap("calc.ico")
abdur.title("Calculator - Made by AbdurCodes")
def calc(event):
global entry_val
btext = event.widget.cget("text")
try:
if btext == "=":
if entry_val.get().isdigit():
value = int(entry_val.get())
else:
value = eval(screen.get())
entry_val.set(value)
screen.update()
elif btext == "CL":
entry_val.set("")
screen.update()
else:
entry_val.set(entry_val.get() + btext)
screen.update()
except Exception as e:
entry_val.set("ERROR!")
screen.update()
# Display frame
f0 = Frame(abdur, bg="green")
entry_val = StringVar()
entry_val.set("")
screen = Entry(f0, textvariable=entry_val, font="verdana 35 bold")
screen.pack(padx=16, pady=19, ipadx=12, ipady=16)
f0.pack(padx=3 ,pady=5)
# Buttons row 1
f1 = Frame(abdur, bg="blue")
b = Button(f1, text="1", padx=8, font="tahoma 19 bold")
b.pack(side=LEFT, padx=20, pady=14)
b.bind("<Button-1>", calc) # binds click of left button of the mouse
b = Button(f1, text="2", padx=9, font="tahoma 19 bold")
b.pack(side=LEFT, padx=20, pady=14)
b.bind("<Button-1>", calc)
b = Button(f1, text="3", padx=9, font="tahoma 19 bold")
b.pack(side=LEFT, padx=20, pady=14)
b.bind("<Button-1>", calc)
b = Button(f1, text="+", padx=9, font="tahoma 19 bold")
b.pack(side=LEFT, padx=20, pady=14)
b.bind("<Button-1>", calc)
f1.pack(padx=3 ,pady=5)
# Buttons row 2
f2 = Frame(abdur, bg="blue")
b = Button(f2, text="4", padx=9, font="tahoma 19 bold")
b.pack(side=LEFT, padx=20, pady=13)
b.bind("<Button-1>", calc)
b = Button(f2, text="5", padx=9, font="tahoma 19 bold")
b.pack(side=LEFT, padx=20, pady=13)
b.bind("<Button-1>", calc)
b = Button(f2, text="6", padx=9, font="tahoma 19 bold")
b.pack(side=LEFT, padx=20, pady=13)
b.bind("<Button-1>", calc)
b = Button(f2, text="-", padx=13, font="tahoma 19 bold")
b.pack(side=LEFT, padx=20, pady=13)
b.bind("<Button-1>", calc)
f2.pack(padx=3 ,pady=5)
# Buttons row 3
f3 = Frame(abdur, bg="blue")
b = Button(f3, text="7", padx=15, font="tahoma 19 bold")
b.pack(side=LEFT, padx=14, pady=14)
b.bind("<Button-1>", calc)
b = Button(f3, text="8", padx=15, font="tahoma 19 bold")
b.pack(side=LEFT, padx=14, pady=14)
b.bind("<Button-1>", calc)
b = Button(f3, text="9", padx=15, font="tahoma 19 bold")
b.pack(side=LEFT, padx=14, pady=14)
b.bind("<Button-1>", calc)
b = Button(f3, text="/", padx=15, font="tahoma 19 bold")
b.pack(side=LEFT, padx=14, pady=14)
b.bind("<Button-1>", calc)
f3.pack(padx=3 ,pady=5)
# Buttons row 4
f4 = Frame(abdur, bg="blue")
b = Button(f4, text="0", padx=14, font="tahoma 19 bold")
b.pack(side=LEFT, padx=18, pady=14)
b.bind("<Button-1>", calc)
b = Button(f4, text=".", padx=14, font="tahoma 19 bold")
b.pack(side=LEFT, padx=18, pady=14)
b.bind("<Button-1>", calc)
b = Button(f4, text="=", padx=14, font="tahoma 19 bold")
b.pack(side=LEFT, padx=18, pady=14)
b.bind("<Button-1>", calc)
b = Button(f4, text="*", padx=14, font="tahoma 19 bold")
b.pack(side=LEFT, padx=18, pady=14)
b.bind("<Button-1>", calc)
f4.pack(padx=3 ,pady=5)
# Buttons row 5
f5 = Frame(abdur, bg="blue")
b = Button(f5, text="CL", padx=8,font="tahoma 19 bold")
b.pack(side=LEFT, padx=14, pady=14)
b.bind("<Button-1>", calc)
b = Button(f5, text="00", padx=8,font="tahoma 19 bold")
b.pack(side=LEFT, padx=14, pady=14)
b.bind("<Button-1>", calc)
b = Button(f5, text="000", padx=8,font="tahoma 19 bold")
b.pack(side=LEFT, padx=14, pady=14)
b.bind("<Button-1>", calc)
b = Button(f5, text="%", padx=8,font="tahoma 19 bold")
b.pack(side=LEFT, padx=14, pady=14)
b.bind("<Button-1>", calc)
f5.pack(padx=3 ,pady=5)
abdur.configure(background="blue")
abdur.mainloop()
Comments
Post a Comment
Write something to CodeWithAbdur!