import tkinter as tk
from tkinter import messagebox  

 
def add_to_expression(value):
    entry.insert(tk.END, value)

  
def calculate_expression():
    try:
        result = eval(entry.get())   
        entry.delete(0, tk.END)   
        entry.insert(0, str(result))   
    except Exception as e:
        messagebox.showerror("Error , wrong operator ")
        entry.delete(0, tk.END)   

 
root = tk.Tk()
root.title("Calculator")
root.geometry("300x400")   
root.configure(bg="#1d7a55")


entry = tk.Entry(root, width=25, justify='right')  
 
  
entry.pack(pady=10)


button_frame = tk.Frame(root)
button_frame.pack()

button_width = 9  
button_height = 5  


for i in range(1, 10):
    tk.Button(
        button_frame, text=str(i), width=button_width, height=button_height, 
        command=lambda i=i: add_to_expression(str(i))
    ).grid(row=(i - 1) // 3, column=(i - 1) % 3)


tk.Button(button_frame, text="0", width=button_width, height=button_height, command=lambda: add_to_expression("0")).grid(row=3, column=1)


tk.Button(button_frame, text="calculate", width=button_width, height=button_height, command=calculate_expression).grid(row=3, column=0)


tk.Button(button_frame, text="delete", width=button_width, height=button_height, command=lambda: entry.delete(0, tk.END)).grid(row=3, column=2)


tk.Button(button_frame, text="+", width=button_width, height=button_height, command=lambda: add_to_expression("+")).grid(row=0, column=3)
tk.Button(button_frame, text="-", width=button_width, height=button_height, command=lambda: add_to_expression("-")).grid(row=1, column=3)
tk.Button(button_frame, text="*", width=button_width, height=button_height, command=lambda: add_to_expression("*")).grid(row=2, column=3)
tk.Button(button_frame, text="/", width=button_width, height=button_height, command=lambda: add_to_expression("/")).grid(row=3, column=3)


root.mainloop()