import tkinter as tk
def on_click(event):text = event.widget.cget("text")
root = tk.Tk()root.title("Simple Calculator")
buttons = ['7', '8', '9', '/','4', '5', '6', '*','1', '2', '3', '-','C', '0', '=', '+']
entry = tk.Entry(root, width=20, borderwidth=5)entry.grid(row=0, column=0, columnspan=4)
row = 1col = 0for button_text in buttons:button = tk.Button(root, text=button_text, padx=30, pady=20)button.grid(row=row, column=col)button.bind("", on_click)col += 1if col > 3:col = 0row += 1
root.mainloop()
import tkinter as tk
def on_click(event):
if text == "C":text = event.widget.cget("text")
entry.delete(0, tk.END)
elif text == "=":
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")
else:
entry.insert(tk.END, text)
root = tk.Tk()
root.title("Simple Calculator")
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'C', '0', '=', '+'
]
entry = tk.Entry(root, width=20, borderwidth=5)
entry.grid(row=0, column=0, columnspan=4)
row = 1
col = 0
for button_text in buttons:
button = tk.Button(root, text=button_text, padx=30, pady=20)
button.grid(row=row, column=col)
button.bind("", on_click)
col += 1
if col > 3:
col = 0
row += 1
root.mainloop()