import tkinter as tk
from tkinter import messagebox
class Calculator:
def __init__(self, root):
[Link] = root
[Link]("Calculator")
[Link]("400x600")
[Link](False, False)
[Link] = ""
self.input_text = [Link]()
[Link](bg="#1e1e1e")
display_frame = [Link]([Link], bg="#1e1e1e")
display_frame.pack(expand=True, fill="both")
display_field = [Link](
display_frame, textvariable=self.input_text, font=("Arial", 24),
justify="right", bd=10, bg="#333333", fg="white"
)
display_field.pack(expand=True, fill="both", padx=10, pady=20)
button_frame = [Link]([Link], bg="#1e1e1e")
button_frame.pack(expand=True, fill="both")
buttons = [
['7', '8', '9', '/'],
['4', '5', '6', '*'],
['1', '2', '3', '-'],
['C', '0', '=', '+']
]
for row in buttons:
row_frame = [Link](button_frame, bg="#1e1e1e")
row_frame.pack(expand=True, fill="both")
for button_text in row:
button = [Link](
row_frame, text=button_text, font=("Arial", 18), width=3,
height=2,
fg="white", bg="#4b4b4b", activebackground="#5c5c5c",
borderwidth=0,
command=lambda text=button_text: self.on_button_click(text)
)
[Link](side="left", expand=True, fill="both", padx=5, pady=5)
[Link]("<Key>", self.on_key_press)
def on_button_click(self, text):
if text == "=":
self.evaluate_expression()
elif text == "C":
self.clear_expression()
else:
[Link] += str(text)
self.input_text.set([Link])
def on_key_press(self, event):
if [Link]() or [Link] in "+-*/":
[Link] += [Link]
self.input_text.set([Link])
elif [Link] == "Return":
self.evaluate_expression()
elif [Link] == "BackSpace":
[Link] = [Link][:-1]
self.input_text.set([Link])
elif [Link]() == "c":
self.clear_expression()
def evaluate_expression(self):
try:
result = str(eval([Link]))
self.input_text.set(result)
[Link] = result
except Exception as e:
[Link]("Error", "Invalid Expression")
self.input_text.set("")
[Link] = ""
def clear_expression(self):
[Link] = ""
self.input_text.set("")
if __name__ == "__main__":
root = [Link]()
calc = Calculator(root)
[Link]()