simple Calculator app

Job ID: 39976523

Budget: ₹100 – ₹400 INR

import tkinter as tk

# Create main window
root = tk.Tk()
root.title("Simple Calculator")
root.geometry("300x400")
root.resizable(False, False)

# Entry widget to display the expression
expression = ""

def press(num):
global expression
expression += str(num)
equation.set(expression)

def clear():
global expression
expression = ""
equation.set("")

def equalpress():
try:
global expression
total = str(eval(expression)) # Evaluate the math expression
equation.set(total)
expression = total
except:
equation.set("Error")
expression = ""

# StringVar() to update entry box dynamically
equation = tk.StringVar()

# Entry field
entry_field = tk.Entry(root, textvariable=equation, font=('Arial', 20), bd=10, insertwidth=2, width=14, borderwidth=4, relief="ridge", justify='right')
entry_field.grid(row=0, column=0, columnspan=4, pady=10)

# Button layout
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)
]

# Create buttons dynamically
for (text, row, col) in buttons:
if text == '=':
tk.Button(root, text=text, padx=20, pady=20, bg="lightgreen", command=equalpress).grid(row=row, column=col, padx=5, pady=5)
else:
tk.Button(root, text=text, padx=20, pady=20, command=lambda t=text: press(t)).grid(row=row, column=col, padx=5, pady=5)

# Clear button
tk.Button(root, text='C', padx=20, pady=20, bg="lightcoral", command=clear).grid(row=5, column=0, columnspan=4, sticky="we", padx=5, pady=5)

# Start the main loop
root.mainloop()