import tkinter as tk def equals_click(): try: lbl_display["text"] = str(round(eval(lbl_display["text"]),5)) except: lbl_display["text"] = lbl_display["text"] def btn_click(value): if value == "C": lbl_display["text"] = "" else: if value == "x": value = "*" #elif value == "÷": # value = "/" lbl_display["text"] += value def delete_button(): lbl_display["text"]=lbl_display["text"][:-1] window = tk.Tk() window.title("Calculator") frm_display = tk.Frame(window, bd = 2) frm_buttons = tk.Frame(window, bd = 2) lbl_display = tk.Label(frm_display, bg = "#E3F8FF", width="20",relief=tk.RAISED) btn_zero = tk.Button(frm_buttons, text="0", width="4",bg = "#EDD9DF", command = lambda:btn_click("0")) btn_one = tk.Button(frm_buttons, text="1",bg = "#EDD9DF", command = lambda:btn_click("1")) btn_two = tk.Button(frm_buttons, text="2",bg = "#EDD9DF", command = lambda:btn_click("2")) btn_three = tk.Button(frm_buttons, text="3",bg = "#EDD9DF", command = lambda:btn_click("3")) btn_four = tk.Button(frm_buttons, text="4",bg = "#EDD9DF", command = lambda:btn_click("4")) btn_five = tk.Button(frm_buttons, text="5",bg = "#EDD9DF", command = lambda:btn_click("5")) btn_six = tk.Button(frm_buttons, text="6",bg = "#EDD9DF", command = lambda:btn_click("6")) btn_seven = tk.Button(frm_buttons, text="7",bg = "#EDD9DF", command = lambda:btn_click("7")) btn_eight = tk.Button(frm_buttons, text="8",bg = "#EDD9DF", command = lambda:btn_click("8")) btn_nine = tk.Button(frm_buttons, text="9",bg = "#EDD9DF", command = lambda:btn_click("9")) btn_plus = tk.Button(frm_buttons, text="+", width="4", command = lambda:btn_click("+")) btn_minus = tk.Button(frm_buttons, text="-", command = lambda:btn_click("-")) #btn_divide = tk.Button(frm_buttons, text="÷", command = lambda:btn_click("÷")) btn_divide = tk.Button(frm_buttons, text="/", command = lambda:btn_click("/")) btn_multiply = tk.Button(frm_buttons, text="x", command = lambda:btn_click("x")) btn_equals = tk.Button(frm_buttons, text="=", width="4", bg = "#CFBBCF", command = lambda:equals_click()) btn_decimalpoint = tk.Button(frm_buttons, text=".", width="4",bg = "#EDD9DF", command = lambda:btn_click(".")) btn_openbracket = tk.Button(frm_buttons, text="(", command = lambda:btn_click("(")) btn_closebracket = tk.Button(frm_buttons, text=")", command = lambda:btn_click(")")) btn_clear = tk.Button(frm_buttons, text="C", command = lambda:btn_click("C")) btn_delete = tk.Button(frm_buttons, text="DEL", command = lambda:delete_button()) btn_openbracket.grid(row=0, column=0, sticky="ew", padx=1, pady=1) btn_closebracket.grid(row=0, column=1, sticky="ew", padx=1, pady=1) btn_clear.grid(row=0, column=2, sticky="ew", padx=1, pady=1) btn_delete.grid(row=0, column=3, sticky="ew", padx=1, pady=1) btn_seven.grid(row=1, column=0, sticky="ew", padx=1, pady=1) btn_eight.grid(row=1, column=1, sticky="ew", padx=1, pady=1) btn_nine.grid(row=1, column=2, sticky="ew", padx=1, pady=1) btn_divide.grid(row=1, column=3, sticky="ew", padx=1, pady=1) btn_four.grid(row=2, column=0, sticky="ew", padx=1, pady=1) btn_five.grid(row=2, column=1, sticky="ew", padx=1, pady=1) btn_six.grid(row=2, column=2, sticky="ew", padx=1, pady=1) btn_multiply.grid(row=2, column=3, sticky="ew", padx=1, pady=1) btn_one.grid(row=3, column=0, sticky="ew", padx=1, pady=1) btn_two.grid(row=3, column=1, sticky="ew", padx=1, pady=1) btn_three.grid(row=3, column=2, sticky="ew", padx=1, pady=1) btn_minus.grid(row=3, column=3, sticky="ew", padx=1, pady=1) btn_zero.grid(row=4, column=0, sticky="ew", padx=1, pady=1) btn_decimalpoint.grid(row=4, column=1, sticky="ew", padx=1, pady=1) btn_equals.grid(row=4, column=2, sticky="ew", padx=1, pady=1) btn_plus.grid(row=4, column=3, sticky="ew", padx=1, pady=1) lbl_display.grid(row=0, column=0, padx=1, pady=1) frm_display.grid(row=0, column=0) frm_buttons.grid(row=1, column=0) window.mainloop()