-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
203 lines (165 loc) · 7.82 KB
/
calculator.py
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import tkinter as tk
from configparser import ConfigParser
def load_theme(theme_name):
wooh = ConfigParser()
wooh.read('theme.ini')
if theme_name not in wooh:
print(f"Theme '{theme_name}' not found. Loading default 'Classic' theme.")
theme_name = 'Classic'
theme = wooh[theme_name]
return {
"OFF_WHITE": theme.get("OFF_WHITE", "#F8FAFF"),
"WHITE": theme.get("WHITE", "#FFFFFF"),
"LIGHT_BLUE": theme.get("LIGHT_BLUE", "#CCEDFF"),
"LIGHT_GRAY": theme.get("LIGHT_GRAY", "#F5F5F5"),
"LABEL_COLOR": theme.get("LABEL_COLOR", "#25265E"),
"BUTTON_TEXT_COLOR": theme.get("BUTTON_TEXT_COLOR", "#25265E"),
"BUTTON_BACKGROUND": theme.get("BUTTON_BACKGROUND", "#CCEDFF"),
"EQUALS_BACKGROUND": theme.get("EQUALS_BACKGROUND", "#4A90E2"),
}
config = ConfigParser()
config.read('theme.ini')
selected_theme = config['default']['theme_name']
colors = load_theme(selected_theme)
LARGE_FONT_STYLE = ("Arial", 40, "bold")
SMALL_FONT_STYLE = ("Arial", 16)
DIGITS_FONT_STYLE = ("Arial", 24, "bold")
DEFAULT_FONT_STYLE = ("Arial", 20)
OFF_WHITE = colors["OFF_WHITE"]
WHITE = colors["WHITE"]
LIGHT_BLUE = colors["LIGHT_BLUE"]
LIGHT_GRAY = colors["LIGHT_GRAY"]
LABEL_COLOR = colors["LABEL_COLOR"]
BUTTON_TEXT_COLOR = colors["BUTTON_TEXT_COLOR"]
BUTTON_BACKGROUND = colors["BUTTON_BACKGROUND"]
EQUALS_BACKGROUND = colors["EQUALS_BACKGROUND"]
class Calculator:
def __init__(self):
self.window = tk.Tk()
self.window.geometry("375x667")
self.window.resizable(0, 0)
self.window.title("Calculator By IH Efty")
self.total_expression = ""
self.current_expression = ""
self.display_frame = self.create_display_frame()
self.total_label, self.label = self.create_display_labels()
self.digits = {
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.operations = {"/": "\u00F7", "*": "\u00D7", "-": "-", "+": "+"}
self.buttons_frame = self.create_buttons_frame()
for x in range(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.evaluate())
for key in self.digits:
self.window.bind(str(key), lambda event, digit=key: self.add_to_expression(digit))
for key in self.operations:
self.window.bind(key, lambda event, operator=key: self.append_operator(operator))
def create_special_buttons(self):
self.create_clear_button()
self.create_equals_button()
self.create_square_button()
self.create_sqrt_button()
self.create_backspace_button()
def create_display_labels(self):
total_label = tk.Label(self.display_frame, text=self.total_expression, anchor=tk.E, bg=LIGHT_GRAY,
fg=LABEL_COLOR, padx=24, font=SMALL_FONT_STYLE)
total_label.pack(expand=True, fill='both')
label = tk.Label(self.display_frame, text=self.current_expression, anchor=tk.E, bg=LIGHT_GRAY,
fg=LABEL_COLOR, padx=24, font=LARGE_FONT_STYLE)
label.pack(expand=True, fill='both')
return total_label, label
def create_display_frame(self):
frame = tk.Frame(self.window, height=221, bg=OFF_WHITE)
frame.pack(expand=True, fill="both")
return frame
def add_to_expression(self, value):
self.current_expression += str(value)
self.update_label()
def create_digit_buttons(self):
for digit, grid_value in self.digits.items():
button = tk.Button(self.buttons_frame, text=str(digit), bg=BUTTON_BACKGROUND, fg=BUTTON_TEXT_COLOR,
font=DIGITS_FONT_STYLE, borderwidth=0, command=lambda x=digit: self.add_to_expression(x))
button.grid(row=grid_value[0], column=grid_value[1], sticky=tk.NSEW)
def append_operator(self, operator):
self.current_expression += operator
self.total_expression += self.current_expression
self.current_expression = ""
self.update_total_label()
self.update_label()
def create_operator_buttons(self):
i = 0
for operator, symbol in self.operations.items():
button = tk.Button(self.buttons_frame, text=symbol, bg=BUTTON_BACKGROUND, fg=BUTTON_TEXT_COLOR,
font=DEFAULT_FONT_STYLE, borderwidth=0, command=lambda x=operator: self.append_operator(x))
button.grid(row=i, column=4, sticky=tk.NSEW)
i += 1
def clear(self):
self.current_expression = ""
self.total_expression = ""
self.update_label()
self.update_total_label()
def create_clear_button(self):
button = tk.Button(self.buttons_frame, text="C", bg=BUTTON_BACKGROUND, fg=BUTTON_TEXT_COLOR,
font=DEFAULT_FONT_STYLE, borderwidth=0, command=self.clear)
button.grid(row=0, column=1, sticky=tk.NSEW)
def backspace(self):
self.current_expression = self.current_expression[:-1]
self.update_label()
def create_backspace_button(self):
button = tk.Button(self.buttons_frame, text="←", bg=BUTTON_BACKGROUND, fg=BUTTON_TEXT_COLOR,
font=DEFAULT_FONT_STYLE, borderwidth=0, command=self.backspace)
button.grid(row=0, column=0, sticky=tk.NSEW)
def square(self):
self.current_expression = str(eval(f"{self.current_expression}**2"))
self.update_label()
def create_square_button(self):
button = tk.Button(self.buttons_frame, text="x\u00b2", bg=BUTTON_BACKGROUND, fg=BUTTON_TEXT_COLOR,
font=DEFAULT_FONT_STYLE, borderwidth=0, command=self.square)
button.grid(row=0, column=2, sticky=tk.NSEW)
def sqrt(self):
self.current_expression = str(eval(f"{self.current_expression}**0.5"))
self.update_label()
def create_sqrt_button(self):
button = tk.Button(self.buttons_frame, text="\u221ax", bg=BUTTON_BACKGROUND, fg=BUTTON_TEXT_COLOR,
font=DEFAULT_FONT_STYLE, borderwidth=0, command=self.sqrt)
button.grid(row=0, column=3, sticky=tk.NSEW)
def evaluate(self):
self.total_expression += self.current_expression
self.update_total_label()
try:
self.current_expression = str(eval(self.total_expression))
self.total_expression = ""
except Exception:
self.current_expression = "Error"
finally:
self.update_label()
def create_equals_button(self):
button = tk.Button(self.buttons_frame, text="=", bg=EQUALS_BACKGROUND, fg=WHITE,
font=DEFAULT_FONT_STYLE, borderwidth=0, command=self.evaluate)
button.grid(row=4, column=3, columnspan=2, sticky=tk.NSEW)
def create_buttons_frame(self):
frame = tk.Frame(self.window)
frame.pack(expand=True, fill="both")
return frame
def update_total_label(self):
expression = self.total_expression
for operator, symbol in self.operations.items():
expression = expression.replace(operator, f' {symbol} ')
self.total_label.config(text=expression)
def update_label(self):
self.label.config(text=self.current_expression[:11])
def run(self):
self.window.mainloop()
if __name__ == "__main__":
calc = Calculator()
calc.run()