Python Code
from tkinter import *
from tkinter import ttk
expression = ""
def btn_entry(num):
global expression
expression = expression + str(num)
equation.set(expression)
def exp_eval():
try:
global expression
expression = str(eval(expression))
equation.set(expression)
except ZeroDivisionError:
print(expression)
equation.set("Division by Zero Error ")
expression = ""
def exp_clear():
global expression
expression = ""
equation.set(expression)
sc = Tk()
sc.title("Calculator")
sc.geometry("480x280")
sc.configure(background ="white")
s = ttk.Style()
print(s.theme_use())
s.theme_use("alt")
#s.theme_use("default")
mainframe = ttk.Frame(sc)
#Resize window
for x in range(10):
sc.columnconfigure(x,weight=x)
sc.rowconfigure(x,weight=x)
equation = StringVar()
s.configure("keys.TButton",font="Arial 16")
key_1 = ttk.Button(sc,style = "keys.TButton",width = 10, text="1", command=lambda:btn_entry(1))
key_1.grid(column=0,row=1, sticky =(E))
key_2 = ttk.Button(sc,style = "keys.TButton",width = 10, text="2", command=lambda:btn_entry(2))
key_2.grid(column=1,row=1, sticky =(E))
key_3 = ttk.Button(sc,style = "keys.TButton",width = 10, text="3", command=lambda:btn_entry(3))
key_3.grid(column=2,row=1, sticky =(E))
key_4 = ttk.Button(sc,style = "keys.TButton",width = 10, text="4", command=lambda:btn_entry(4))
key_4.grid(column=0,row=2, sticky =(E))
key_5 = ttk.Button(sc,style = "keys.TButton",width = 10, text="5", command=lambda:btn_entry(5))
key_5.grid(column=1,row=2, sticky =(E))
key_6 = ttk.Button(sc,style = "keys.TButton",width = 10, text="6", command=lambda:btn_entry(6))
key_6.grid(column=2,row=2, sticky =(E))
key_7 = ttk.Button(sc,style = "keys.TButton",width = 10, text="7", command=lambda:btn_entry(7))
key_7.grid(column=0,row=3, sticky =(E))
key_8 = ttk.Button(sc,style = "keys.TButton",width = 10, text="8", command=lambda:btn_entry(8))
key_8.grid(column=1,row=3, sticky =(E))
key_9 = ttk.Button(sc,style = "keys.TButton",width = 10, text="9", command=lambda:btn_entry(9))
key_9.grid(column=2,row=3, sticky =(E))
key_0 = ttk.Button(sc,style = "keys.TButton",width = 10, text="0", command=lambda:btn_entry(0))
key_0.grid(column=1,row=4, sticky =(E))
key_dec = ttk.Button(sc,style = "keys.TButton",width = 10, text=".", command=lambda:btn_entry("."))
key_dec.grid(column=0,row=4, sticky =(E))
key_add = ttk.Button(sc,style = "keys.TButton",width = 10, text="+", command=lambda:btn_entry("+"))
key_add.grid(column=3,row=1, sticky =(N))
key_sub = ttk.Button(sc,style = "keys.TButton",width = 10, text="-", command=lambda:btn_entry("-"))
key_sub.grid(column=3,row=2, sticky =(N))
key_mul = ttk.Button(sc,style = "keys.TButton",width = 10, text="*", command=lambda:btn_entry("*"))
key_mul.grid(column=3,row=3, sticky =(N))
key_div = ttk.Button(sc,style = "keys.TButton",width = 10, text="/", command=lambda:btn_entry("/"))
key_div.grid(column=3,row=4, sticky =(N))
s.configure('equal.TButton',background='light green' ,font="Arial 16", embossed = 1)
key_equal = ttk.Button(sc,style = "equal.TButton", text="=",width = 30, command=lambda:exp_eval())
key_equal.grid(column=1,row=5,columnspan=3, sticky=(W))
s.configure('clear.TButton',background='pink' ,font="Arial 16", embossed = 1)
key_clr = ttk.Button(sc, text="CLEAR", style="clear.TButton",width = 10, command=lambda:exp_clear())
key_clr.grid(column=0,row=5, sticky =(E))
s.configure("display.TLabel",background ='yellow' ,padding =20 ,font = "Arial 24")
exp_display = ttk.Label(sc, textvariable = equation, style = "display.TLabel")
exp_display.grid(column=0,columnspan =3,row = 0,sticky=(S,E))
exp_display.columnconfigure(1,weight = 2)
sc.mainloop()
Leave a Reply