-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyCalc.py
360 lines (306 loc) · 12.2 KB
/
pyCalc.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# Modules
import customtkinter as ct
from PIL import Image
# App appearance mode, settings & more
ct.set_appearance_mode("System")
app = ct.CTk(fg_color="black")
app.title("pyCalculator")
app.geometry(f"{425}x{680}")
app.resizable(False, False)
# Fonts & Colors
monst = ct.CTkFont(family="Montserrat",size=80,weight="bold")
monst2 = ct.CTkFont(family="Montserrat",size=20,weight="bold")
dark_grey = "#454545"
light_grey = "#999999"
lighter = "#AFAABB"
darker = "#696379"
purple = "#AA00FF"
mid = "#7965B0"
# App Logo
pyCalc_logo = ct.CTkImage(dark_image=Image.open("pylogo.png"), size=(600, 90))
# App Frames
frame1 = ct.CTkFrame(app, fg_color="black")
frame1.pack(side="top", expand=True, fill="both", padx=5, pady=5)
frame2 = ct.CTkFrame(app, fg_color="black")
frame2.pack(side="bottom", expand=True, fill="both", padx=5, pady=5)
# Entry/title/Logo
Image_Label = ct.CTkLabel(master=frame1,
text="",
image=pyCalc_logo,
font=monst,
text_color="white",
width=425, height=90
)
Image_Label.pack(expand=True, fill="both", side="top")
entry = ct.CTkEntry(master=frame1,
justify="right",
state="normal",
height=150,
fg_color="black",
font=monst,
placeholder_text="0",
placeholder_text_color="white",
border_color="black"
)
entry.pack(expand=True, fill="both", side="bottom", pady=5, padx=5)
# --- FUNCTIONS ---
expression = ""
results = ""
# Function ---> update AC button and command
def update_All_Clear_Btn():
global expression, results
if not expression or results:
All_Clear_Btn.configure(text="AC", command=all_clear)
else:
All_Clear_Btn.configure(text="C", command=clear_single_char)
# Function ---> Pressed button
def pressed(char):
global expression
# Grab button values, & extend cursor
current_entry = entry.get()
new_entry = current_entry + char
entry.delete(0, ct.END)
entry.insert(0, new_entry)
expression = new_entry
entry.xview_moveto(1.0)
entry.icursor(len(new_entry))
update_All_Clear_Btn()
# Function ---> All Clear (AC)
def all_clear():
global expression, results
entry.delete(0, ct.END)
expression = ""
update_All_Clear_Btn()
entry.configure(placeholder_text="0",
placeholder_text_color="white")
# Function ---> Equals
def equals():
global expression, results
# Replace customs, eval & Update entry
try:
expression = expression.replace("÷", "/").replace("x", "*").replace("−", "-").replace("%", "/100")
results = str(eval(expression))
entry.delete(0, ct.END)
entry.insert(0, results)
expression = results
entry.xview_moveto(1.0)
entry.icursor(len(results))
update_All_Clear_Btn()
expression = ""
results = ""
adjust_font_size(entry)
# Handle errors
except Exception as e:
handle_error()
# Function ---> Error Handling & Update AC
def handle_error():
global expression, results
entry.delete(0, ct.END)
entry.insert(0, "Error")
expression = ""
results = ""
update_All_Clear_Btn()
# Function ---> Toggle/Alter sign(+/-) at cursor
def alter_sign():
current_entry = entry.get()
index = entry.index(ct.INSERT)
# Check if entry is void, or cursor, at start
if current_entry == "" or index == 0:
entry.insert(index, "-")
else:
# Find nearesr sign before cursor
nearest_sign_index = max(current_entry.rfind('+', 0, index),
current_entry.rfind('-', 0, index),
current_entry.rfind('x', 0, index),
current_entry.rfind('÷', 0, index))
# Remove "-" at nearest sign if negative, and the signs is odd number, b4 cursor
if nearest_sign_index >= 0 and current_entry[nearest_sign_index] == '-' \
and current_entry[nearest_sign_index + 1:index].count('-') % 2 == 1:
entry.delete(nearest_sign_index + 1)
else:
# Insert/Remove "-" based on index
if current_entry[index-1] == '-':
entry.delete(index-1)
else:
entry.insert(index, "-")
# Function ---> Clear Single char (C)
def clear_single_char():
global expression
current_entry = entry.get()
if current_entry:
new_entry = current_entry[:-1]
entry.delete(0, ct.END)
entry.insert(0, new_entry)
expression = new_entry
update_All_Clear_Btn()
entry.configure(placeholder_text="0",
placeholder_text_color="white")
# Function ---> Adjust results to fit dynamically
def adjust_font_size(widget):
max_font_size = 80
min_font_size = 20
max_width = widget.winfo_width() - 20
text = widget.get()
# Calculate font_size based on text len & entry width
font_size = max(min_font_size, min(max_font_size, int(max_width / len(text))))
# Configure to Monstserrat
widget.configure(font=("Montserrat", font_size, "bold"))
# --- OTHER WIDGETS---
# Buttons /1
All_Clear_Btn = ct.CTkButton(master=frame2, text="AC",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=light_grey,
hover_color=lighter,
command=all_clear)
All_Clear_Btn.grid(row=0, column=0, sticky="nswe", pady=5, padx=5)
Btn_7 = ct.CTkButton(master=frame2, text="7",
corner_radius=50, width=93.75, height=68,
font=monst2,
fg_color=dark_grey,
hover_color=darker,
command=lambda: pressed("7"))
Btn_7.grid(row=1, column=0, sticky="nswe", pady=5, padx=5)
Btn_4 = ct.CTkButton(master=frame2, text="4",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=dark_grey,
hover_color=darker,
command=lambda: pressed("4"))
Btn_4.grid(row=2, column=0, sticky="nswe", pady=5, padx=5)
Btn_1= ct.CTkButton(master=frame2, text="1",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=dark_grey,
hover_color=darker,
command=lambda: pressed("1"))
Btn_1.grid(row=3, column=0, sticky="nswe", pady=5, padx=5)
Btn_0 = ct.CTkButton(master=frame2, text="0",
corner_radius=50,
width=93.75, height=68,
font=monst2,
anchor="w",
fg_color=dark_grey,
hover_color=darker,
command=lambda: pressed("0"))
Btn_0.grid(row=4, column=0, sticky="nswe", pady=5, columnspan=2, padx=5)
# Buttons /2
sign_Btn = ct.CTkButton(master=frame2, text="+/-",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=light_grey,
hover_color=lighter,
command=alter_sign)
sign_Btn.grid(row=0, column=1, sticky="nswe", pady=5, padx=5)
Btn_8 = ct.CTkButton(master=frame2, text="8",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=dark_grey,
hover_color=darker,
command=lambda: pressed("8"))
Btn_8.grid(row=1, column=1, sticky="nswe", pady=5, padx=5)
Btn_5= ct.CTkButton(master=frame2, text="5",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=dark_grey,
hover_color=darker,
command=lambda: pressed("5"))
Btn_5.grid(row=2, column=1, sticky="nswe", pady=5, padx=5)
Btn_2= ct.CTkButton(master=frame2, text="2",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=dark_grey,
hover_color=darker,
command=lambda: pressed("2"))
Btn_2.grid(row=3, column=1, sticky="nswe", pady=5, padx=5)
# Buttons /3
percent_Btn = ct.CTkButton(master=frame2, text="%",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=light_grey,
hover_color=lighter,
command=lambda: pressed("%"))
percent_Btn.grid(row=0, column=2, sticky="nswe", pady=5, padx=5)
Btn_9 = ct.CTkButton(master=frame2, text="9",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=dark_grey,
hover_color=darker,
command=lambda: pressed("9"))
Btn_9.grid(row=1, column=2, sticky="nswe", pady=5, padx=5)
Btn_6 = ct.CTkButton(master=frame2, text="6",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=dark_grey,
hover_color=darker,
command=lambda: pressed("6"))
Btn_6.grid(row=2, column=2, sticky="nswe", pady=5, padx=5)
Btn_3= ct.CTkButton(master=frame2, text="3",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=dark_grey,
hover_color=darker,
command=lambda: pressed("3"))
Btn_3.grid(row=3, column=2, sticky="nswe", pady=5, padx=5)
point_Btn = ct.CTkButton(master=frame2, text=".",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=dark_grey,
hover_color=darker,
command=lambda: pressed("."))
point_Btn.grid(row=4, column=2, sticky="nswe", pady=5, padx=5)
# Buttons /4
div_Btn = ct.CTkButton(master=frame2, text="÷",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=purple,
hover_color=mid,
command=lambda: pressed("÷"))
div_Btn.grid(row=0, column=3, sticky="nswe", pady=5, padx=5)
mul_Btn = ct.CTkButton(master=frame2, text="x",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=purple,
hover_color=mid,
command=lambda: pressed("x"))
mul_Btn.grid(row=1, column=3, sticky="nswe", pady=5, padx=5)
minus_Btn = ct.CTkButton(master=frame2, text="−",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=purple,
hover_color=mid,
command=lambda: pressed("−"))
minus_Btn.grid(row=2, column=3, sticky="nswe", pady=5, padx=5)
add_Btn = ct.CTkButton(master=frame2, text="+",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=purple,
hover_color=mid,
command=lambda: pressed("+"))
add_Btn.grid(row=3, column=3, sticky="nswe", pady=5, padx=5)
equal_Btn = ct.CTkButton(master=frame2, text="=",
corner_radius=50,
width=93.75, height=68,
font=monst2,
fg_color=purple,
hover_color=mid,
command=equals)
equal_Btn.grid(row=4, column=3, sticky="nswe", pady=5, padx=5)
# Run app (as admin😁)
app.mainloop()