-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
180 lines (148 loc) · 7.43 KB
/
calculator.py
File metadata and controls
180 lines (148 loc) · 7.43 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import tkinter as tk
# Define theme colors
FONT_LARGE = ("Arial", 40, "bold")
FONT_SMALL = ("Arial", 16)
FONT_DIGITS = ("Arial", 24, "bold")
FONT_DEFAULT = ("Arial", 20)
COLOR_BACKGROUND = "#000000" # Black background color for the window
COLOR_DISPLAY_BACKGROUND = "#000000" # Black background color for the display area
COLOR_TEXT = "#FFFFFF" # White text color
COLOR_BUTTON = "#333333" # Dark gray button color
COLOR_BUTTON_TEXT = "#000000" # Black button text color
COLOR_BUTTON_HOVER = "#444444" # Darker gray button hover color
class Calculator:
def __init__(self):
self.window = tk.Tk()
self.window.geometry("375x667")
self.window.resizable(0, 0)
self.window.title("Calculator")
self.window.configure(bg=COLOR_BACKGROUND)
self.total_expr = ""
self.current_expr = ""
self.display_frame = self.create_display_frame()
self.total_label, self.current_label = self.create_display_labels()
self.digit_positions = {
7: (1, 1), 8: (1, 2), 9: (1, 3),
4: (2, 1), 5: (2, 2), 6: (2, 3),
1: (3, 1), 2: (3, 2), 3: (3, 3),
0: (4, 2), '.': (4, 1)
}
self.operators = {"/": "\u00F7", "*": "\u00D7", "-": "-", "+": "+"}
self.buttons_frame = self.create_buttons_frame()
self.buttons_frame.rowconfigure(0, weight=1)
for x in range(1, 5):
self.buttons_frame.rowconfigure(x, weight=1)
self.buttons_frame.columnconfigure(x, weight=1)
self.create_digit_buttons()
self.create_operator_buttons()
self.create_special_buttons()
self.bind_keys()
def bind_keys(self):
self.window.bind("<Return>", lambda event: self.calculate_result())
for key in self.digit_positions:
self.window.bind(str(key), lambda event, digit=key: self.add_to_expression(digit))
for key in self.operators:
self.window.bind(key, lambda event, operator=key: self.add_operator(operator))
def create_special_buttons(self):
self.create_clear_button()
self.create_equals_button()
self.create_square_button()
self.create_sqrt_button()
def create_display_labels(self):
total_label = tk.Label(self.display_frame, text=self.total_expr, anchor=tk.E, bg=COLOR_DISPLAY_BACKGROUND,
fg=COLOR_TEXT, padx=24, font=FONT_SMALL)
total_label.pack(expand=True, fill='both')
current_label = tk.Label(self.display_frame, text=self.current_expr, anchor=tk.E, bg=COLOR_DISPLAY_BACKGROUND,
fg=COLOR_TEXT, padx=24, font=FONT_LARGE)
current_label.pack(expand=True, fill='both')
return total_label, current_label
def create_display_frame(self):
frame = tk.Frame(self.window, height=221, bg=COLOR_DISPLAY_BACKGROUND)
frame.pack(expand=True, fill="both")
return frame
def add_to_expression(self, value):
self.current_expr += str(value)
self.update_current_label()
def create_digit_buttons(self):
for digit, pos in self.digit_positions.items():
button = tk.Button(self.buttons_frame, text=str(digit), bg=COLOR_BUTTON, fg=COLOR_BUTTON_TEXT, font=FONT_DIGITS,
borderwidth=0, command=lambda x=digit: self.add_to_expression(x))
button.grid(row=pos[0], column=pos[1], sticky=tk.NSEW)
button.bind("<Enter>", lambda e, btn=button: btn.config(bg=COLOR_BUTTON_HOVER))
button.bind("<Leave>", lambda e, btn=button: btn.config(bg=COLOR_BUTTON))
def add_operator(self, operator):
self.current_expr += operator
self.total_expr += self.current_expr
self.current_expr = ""
self.update_total_label()
self.update_current_label()
def create_operator_buttons(self):
i = 0
for operator, symbol in self.operators.items():
button = tk.Button(self.buttons_frame, text=symbol, bg=COLOR_BUTTON, fg=COLOR_BUTTON_TEXT, font=FONT_DEFAULT,
borderwidth=0, command=lambda x=operator: self.add_operator(x))
button.grid(row=i, column=4, sticky=tk.NSEW)
button.bind("<Enter>", lambda e, btn=button: btn.config(bg=COLOR_BUTTON_HOVER))
button.bind("<Leave>", lambda e, btn=button: btn.config(bg=COLOR_BUTTON))
i += 1
def clear_all(self):
self.current_expr = ""
self.total_expr = ""
self.update_current_label()
self.update_total_label()
def create_clear_button(self):
button = tk.Button(self.buttons_frame, text="C", bg=COLOR_BUTTON, fg=COLOR_BUTTON_TEXT, font=FONT_DEFAULT,
borderwidth=0, command=self.clear_all)
button.grid(row=0, column=1, sticky=tk.NSEW)
button.bind("<Enter>", lambda e, btn=button: btn.config(bg=COLOR_BUTTON_HOVER))
button.bind("<Leave>", lambda e, btn=button: btn.config(bg=COLOR_BUTTON))
def calculate_square(self):
self.current_expr = str(eval(f"{self.current_expr}**2"))
self.update_current_label()
def create_square_button(self):
button = tk.Button(self.buttons_frame, text="x\u00b2", bg=COLOR_BUTTON, fg=COLOR_BUTTON_TEXT, font=FONT_DEFAULT,
borderwidth=0, command=self.calculate_square)
button.grid(row=0, column=2, sticky=tk.NSEW)
button.bind("<Enter>", lambda e, btn=button: btn.config(bg=COLOR_BUTTON_HOVER))
button.bind("<Leave>", lambda e, btn=button: btn.config(bg=COLOR_BUTTON))
def calculate_sqrt(self):
self.current_expr = str(eval(f"{self.current_expr}**0.5"))
self.update_current_label()
def create_sqrt_button(self):
button = tk.Button(self.buttons_frame, text="\u221ax", bg=COLOR_BUTTON, fg=COLOR_BUTTON_TEXT, font=FONT_DEFAULT,
borderwidth=0, command=self.calculate_sqrt)
button.grid(row=0, column=3, sticky=tk.NSEW)
button.bind("<Enter>", lambda e, btn=button: btn.config(bg=COLOR_BUTTON_HOVER))
button.bind("<Leave>", lambda e, btn=button: btn.config(bg=COLOR_BUTTON))
def calculate_result(self):
self.total_expr += self.current_expr
self.update_total_label()
try:
self.current_expr = str(eval(self.total_expr))
self.total_expr = ""
except Exception:
self.current_expr = "Error"
finally:
self.update_current_label()
def create_equals_button(self):
button = tk.Button(self.buttons_frame, text="=", bg=COLOR_BUTTON, fg=COLOR_BUTTON_TEXT, font=FONT_DEFAULT,
borderwidth=0, command=self.calculate_result)
button.grid(row=4, column=3, columnspan=2, sticky=tk.NSEW)
button.bind("<Enter>", lambda e, btn=button: btn.config(bg=COLOR_BUTTON_HOVER))
button.bind("<Leave>", lambda e, btn=button: btn.config(bg=COLOR_BUTTON))
def create_buttons_frame(self):
frame = tk.Frame(self.window)
frame.pack(expand=True, fill="both")
return frame
def update_total_label(self):
expr = self.total_expr
for operator, symbol in self.operators.items():
expr = expr.replace(operator, f' {symbol} ')
self.total_label.config(text=expr)
def update_current_label(self):
self.current_label.config(text=self.current_expr[:11])
def run(self):
self.window.mainloop()
if __name__ == "__main__":
calculator = Calculator()
calculator.run()