import tkinter as tk
from tkinter import messagebox
def click_button(button_text):
current_text = entry.get()
entry.delete(0, tk.END)
entry.insert(0, current_text + button_text)
def clear_entry():
entry.delete(0, tk.END)
def calculate():
try:
result = eval(entry.get())
entry.delete(0, tk.END)
entry.insert(0, str(result))
except:
messagebox.showerror("Error", "Invalid Expression")
# Create GUI Window
root = tk.Tk()
root.title("Simple Calculator")
root.geometry("320x420")
root.configure(bg="#1e1e2e")
entry = tk.Entry(root, width=20, font=("Arial", 20), bd=5, relief=tk.FLAT, justify='right')
entry.grid(row=0, column=0, columnspan=4, pady=10, padx=10)
entry.configure(bg="#f8f8f2", fg="#282a36")
button_style = {"font": ("Arial", 16), "bd": 3, "relief": tk.RAISED, "width": 5, "height": 2}
buttons = [
('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3),
('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3),
('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3),
('0', 4, 0), ('.', 4, 1), ('=', 4, 2), ('+', 4, 3),
]
for text, row, col in buttons:
if text == '=':
btn = tk.Button(root, text=text, command=calculate, **button_style, bg="#50fa7b", fg="#282a36")
else:
btn = tk.Button(root, text=text, command=lambda t=text: click_button(t), **button_style, bg="#44475a", fg="#f8f8f2")
btn.grid(row=row, column=col, padx=5, pady=5)
tk.Button(root, text="C", command=clear_entry, **button_style, bg="#ff5555", fg="#f8f8f2").grid(row=5, column=0, columnspan=4, pady=10, ipadx=50)
root.mainloop()