-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.py
More file actions
43 lines (36 loc) · 1.5 KB
/
Calculator.py
File metadata and controls
43 lines (36 loc) · 1.5 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
import tkinter as tk
class Calculator:
def __init__(self, root):
self.root = root
self.root.title("Simple Calculator")
self.entry = tk.Entry(root, width=20, font=("Arial", 18), borderwidth=5, relief=tk.RIDGE)
self.entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
self.create_buttons()
def create_buttons(self):
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 text, row, col in buttons:
button = tk.Button(self.root, text=text, font=("Arial", 14), width=5, height=2, command=lambda t=text: self.on_button_click(t))
button.grid(row=row, column=col, padx=5, pady=5)
def on_button_click(self, char):
if char == 'C':
self.entry.delete(0, tk.END)
elif char == '=':
try:
result = eval(self.entry.get())
self.entry.delete(0, tk.END)
self.entry.insert(tk.END, result)
except Exception as e:
self.entry.delete(0, tk.END)
self.entry.insert(tk.END, "Error")
else:
self.entry.insert(tk.END, char)
if __name__ == "__main__":
root = tk.Tk()
calculator = Calculator(root)
root.mainloop()