-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalc.py
133 lines (104 loc) · 3.12 KB
/
calc.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
# A stylized calculator created by Jacob Humston
######################### Modules
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import math
import re
######################### Static Variables
button_characters = [
"Next",
"Previous",
"Back",
"Clear",
"(",
")",
"%",
"/",
"7",
"8",
"9",
"*",
"4",
"5",
"6",
"+",
"1",
"2",
"3",
"—",
"-",
"0",
".",
"=",
]
operators = ["%", "/", "*", "—", "+"]
operators_priority = {"%": 2, "/": 2, "*": 2, "—": 1, "+": 1}
######################### Create the main window.
window = tk.Tk()
window.title("Calculator")
window.iconphoto(False, tk.PhotoImage(file="assets/icon.png"))
window.columnconfigure(1, weight=1)
window.rowconfigure(1, weight=1)
window.resizable(width=False, height=False)
######################### Utility functions.
# Display an error popup.
def display_error(message: str) -> None:
messagebox.showerror("Calculator | ERROR!", message)
return None
# Convert a float to a string safely.
def better_float(var: str) -> float | None:
value = None
try:
value = float(var)
except:
value = None
return value
# Modify a number string to be displayed.
def modify_equation_string(string: str) -> str:
new_string = string
new_string = re.sub("(.{15})", "\\1\n", new_string, 0, re.DOTALL)
new_string = new_string.removesuffix(".0")
for operator in operators:
new_string = new_string.replace(operator, f" {operator} ")
return new_string
######################### Create the button frame.
main_frame = ttk.Frame(window)
main_frame.grid(row=1, column=0, columnspan=8)
main_frame.configure(border=10)
######################### Create the text label.
label = ttk.Label(main_frame, width=20)
label.grid(row=0, column=0, columnspan=8)
label.configure(padding=10, font=("Arial", 20))
######################### Function to add a character to the current label.
current_equation = "0"
equation_history = []
# Add a character to the equation.
def add_character_to_equation(character: str) -> None:
global current_equation
if current_equation == "0":
current_equation = ""
current_equation = f"{current_equation}{character}"
label.configure(text=modify_equation_string(current_equation))
return None
######################### Create buttons.
# Function to create the calculator buttons.
def create_buttons():
column_index = 0
last_row = -1
for index, character in enumerate(button_characters):
row = math.floor(index / 4) + 1
if last_row != row:
column_index = 0
last_row = row
column_index = column_index + 1
column = column_index
def command_function(character=character):
add_character_to_equation(character)
button = ttk.Button(main_frame, text=character, command=command_function)
button.grid(row=row, column=column, sticky="nesw")
if character == "":
button.grid_remove()
create_buttons() # Create the calculator buttons.
######################### Start the process.
window.mainloop()