Create a calculator using python
import tkinter as tk
def button_click(symbol):
if symbol == '=':
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")
elif symbol == 'C':
entry.delete(0, tk.END)
else:
entry.insert(tk.END, symbol)
root = tk.Tk()
root.title("Simple Calculator")
entry = tk.Entry(root, width=20, font=('Arial', 14))
entry.grid(row=0, column=0, columnspan=4)
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),
('C', 5, 0)
]
for (symbol, row, column) in buttons:
button = tk.Button(root, text=symbol, width=5, height=2, font=('Arial', 14),
command=lambda s=symbol: button_click(s))
button.grid(row=row, column=column)
root.mainloop()
Post a Comment