#  واجهة Tkinter
import tkinter as tk

#  العمليات الحسابية
def calculate(operation):
    global current_input
    if operation == 'C':
        current_input = ''
        entry.delete(0, tk.END)
    elif operation == '=':
        try:
            result = eval(current_input)
            entry.delete(0, tk.END)
            entry.insert(0, result)
            current_input = str(result)
        except Exception as e:
            current_input = ''
            entry.delete(0, tk.END)
            entry.insert(0, 'خطأ')
    else:
        current_input += str(operation)
        entry.delete(0, tk.END)
        entry.insert(0, current_input)

#  نافذة التطبيق
root = tk.Tk()
root.title("Calculator")

#  لتخزين الإدخال 
current_input = ''

#  مربع نص لإدخال التعبيرات الحسابية
entry = tk.Entry(root, font=("Arial", 16), justify='right')
entry.grid(row=0, column=0, columnspan=4, pady=10)

#  الأزرار
buttons = [
    ('7', 1, 0), ('8', 1, 1), ('9', 1, 2),
    ('4', 2, 0), ('5', 2, 1), ('6', 2, 2),
    ('1', 3, 0), ('2', 3, 1), ('3', 3, 2),
    ('0', 4, 1), ('+', 1, 3), ('-', 2, 3),
    ('*', 3, 3), ('/', 4, 3), ('=', 4, 2),
    ('C', 4, 0)
]

for (text, row, col) in buttons:
    button = tk.Button(root, text=text, command=lambda t=text: calculate(t), font=("Arial", 16))
    button.grid(row=row, column=col, sticky='nsew')

#  حجم الأزرار
for i in range(1, 5):
    root.grid_rowconfigure(i, weight=1)
    root.grid_columnconfigure(i, weight=1)

# تشغيل النافذة
root.mainloop()
