-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
45 lines (38 loc) · 1.03 KB
/
calculator.py
File metadata and controls
45 lines (38 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import tkinter as tk
def button_click(event):
current = entry.get()
text = event.widget.cget("text")
if text == "=":
try:
result = eval(current)
entry.delete(0, tk.END)
entry.insert(tk.END, str(result))
except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END, "Error")
elif text == "C":
entry.delete(0, tk.END)
else:
entry.insert(tk.END, text)
root = tk.Tk()
root.title("Simple Calculator")
entry = tk.Entry(root, font=("Arial", 18))
entry.grid(row=0, column=0, columnspan=4)
buttons = [
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+",
"C"
]
row = 1
col = 0
for button in buttons:
btn = tk.Button(root, text=button, font=("Arial", 14), padx=20, pady=20)
btn.grid(row=row, column=col, padx=5, pady=5)
btn.bind("<Button-1>", button_click)
col += 1
if col > 3:
col = 0
row += 1
root.mainloop()