import tkinter as tk

def button_click(symbol):
    current = entry.get()
    entry.delete(0, tk.END)
    entry.insert(tk.END, current + symbol)

def clear():
    entry.delete(0, tk.END)

def calculate():
    try:
        result = eval(entry.get())
        entry.delete(0, tk.END)
        entry.insert(tk.END, str(result))
    except:
        entry.delete(0, tk.END)
        entry.insert(tk.END, "Error")

# Create main window
root = tk.Tk()
root.title("Calculator")
root.configure(bg="#6495ED")  # Blue background



# Entry widget to display input/output
entry = tk.Entry(root, width=35, borderwidth=5)
entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)

# Define buttons
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),
]

# Add buttons to the grid
for (text, row, col) in buttons:
    button = tk.Button(root, text=text, padx=30, pady=20, command=lambda symbol=text: button_click(symbol))
    button.grid(row=row, column=col)

# Clear button
clear_button = tk.Button(root, text='Clear', padx=40, pady=20, command=clear)
clear_button.grid(row=5, column=0, columnspan=2)

# Calculate button
calculate_button = tk.Button(root, text='Calculate', padx=45, pady=20, command=calculate)
calculate_button.grid(row=5, column=2, columnspan=2)

# Start the GUI
root.mainloop()
