Skip to content

Commit 4281ff8

Browse files
committed
Initial commit
Creates the basic project structure and implements the initial version of the calculator.
1 parent 2107c0d commit 4281ff8

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

calculator/api/calculator.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class Calculator:
2+
def __init__(self):
3+
self.equation_first = ""
4+
self.equation_last = ""
5+
self.equation_type = ""
6+
self.current_part = "first"
7+
8+
def input_button(self, button: str):
9+
if button.isdigit():
10+
self.add_digit(button)
11+
elif button in ["+", "-", "/", "x"]:
12+
self.set_operator(button)
13+
elif button == "=":
14+
if self.current_part == "last":
15+
self.solve()
16+
elif button == ".":
17+
self.add_decimal()
18+
elif button == "+/-":
19+
self.flip_sign()
20+
21+
def add_digit(self, digit):
22+
if self.current_part == "first":
23+
self.equation_first += digit
24+
elif self.current_part == "last":
25+
self.equation_last += digit
26+
27+
def set_operator(self, operator):
28+
self.equation_type = operator
29+
if self.current_part == "first":
30+
self.current_part = "last"
31+
32+
def flip_sign(self):
33+
if self.current_part == "first":
34+
if self.equation_first.startswith("-"):
35+
self.equation_first = self.equation_first.removeprefix("-")
36+
else:
37+
self.equation_first = "-" + self.equation_first
38+
elif self.current_part == "last":
39+
if self.equation_last.startswith("-"):
40+
self.equation_last = self.equation_last.removeprefix("-")
41+
else:
42+
self.equation_last = "-" + self.equation_last
43+
44+
def add_decimal(self):
45+
if self.current_part == "first":
46+
if not "." in self.equation_first:
47+
self.equation_first += "."
48+
if self.current_part == "last":
49+
if not "." in self.equation_last:
50+
self.equation_last += "."
51+
52+
def solve(self):
53+
result = float(self.equation_first)
54+
55+
if self.equation_type == "+":
56+
result += float(self.equation_last)
57+
elif self.equation_type == "-":
58+
result -= float(self.equation_last)
59+
elif self.equation_type == "/":
60+
result /= float(self.equation_last)
61+
elif self.equation_type == "x":
62+
result *= float(self.equation_last)
63+
64+
if result == int(result):
65+
self.equation_first = str(int(result))
66+
else:
67+
self.equation_first = str(result)
68+
69+
self.equation_last = ""
70+
self.equation_type = ""
71+
self.current_part = "first"

calculator/gui/button.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from api import calculator as calc
2+
import tkinter as tk
3+
4+
LAYOUT_STANDARD = ["AC", "CE", "%", "/",
5+
"7", "8", "9", "x",
6+
"4", "5", "6", "-",
7+
"1", "2", "3", "+",
8+
"+/-", "0", ".", "="]
9+
10+
def calc_button(root, calculator: calc.Calculator, display_text):
11+
button = tk.Button(root, text=display_text, command=lambda: calculator.input_button(display_text))
12+
return button
13+
14+
class CalculatorButtons:
15+
def __init__(self, calculator: calc.Calculator):
16+
self.calculator = calculator
17+
self.frame = None
18+
19+
def create_gui(self, root, layout=LAYOUT_STANDARD, width=4):
20+
self.frame = tk.Frame(root)
21+
22+
for i, label in enumerate(layout):
23+
button = calc_button(self.frame, self.calculator, label)
24+
button.grid(column=i%width, row=int(i/width), sticky=tk.NSEW)
25+
26+
for i in range(width):
27+
self.frame.grid_columnconfigure(i, uniform="calc_buttons")
28+
29+
def update(self):
30+
pass

calculator/gui/display.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from api import calculator as calc
2+
import tkinter as tk
3+
4+
class CalculatorDisplay:
5+
def __init__(self, calculator: calc.Calculator):
6+
self.calculator = calculator
7+
self.frame = None
8+
self.upper_entry = None
9+
self.lower_entry = None
10+
11+
def create_gui(self, root):
12+
self.frame = tk.Frame(root)
13+
self.upper_entry = tk.Entry(self.frame, state=tk.DISABLED, justify=tk.RIGHT)
14+
self.lower_entry = tk.Entry(self.frame, state=tk.DISABLED, justify=tk.RIGHT)
15+
self.upper_entry.grid(row=0, sticky=tk.NSEW)
16+
self.lower_entry.grid(row=1, sticky=tk.NSEW)
17+
18+
def update(self):
19+
self.upper_entry.configure(state=tk.NORMAL)
20+
self.lower_entry.configure(state=tk.NORMAL)
21+
22+
self.upper_entry.delete(0,tk.END)
23+
self.lower_entry.delete(0,tk.END)
24+
25+
if self.calculator.current_part == "first":
26+
self.lower_entry.insert(0,self.calculator.equation_first)
27+
elif self.calculator.current_part == "last":
28+
self.upper_entry.insert(0,self.calculator.equation_first + " " + self.calculator.equation_type)
29+
self.lower_entry.insert(0,self.calculator.equation_last)
30+
31+
self.upper_entry.configure(state=tk.DISABLED)
32+
self.lower_entry.configure(state=tk.DISABLED)
33+

calculator/main.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import tkinter as tk
2+
from gui import button, display
3+
from api import calculator as calc
4+
5+
if __name__ == "__main__":
6+
c = calc.Calculator()
7+
main = tk.Tk()
8+
main.title = "Calculator"
9+
main.grid_rowconfigure(0, weight=1)
10+
main.grid_rowconfigure(1, weight=2)
11+
main.grid_columnconfigure(0, weight=1)
12+
13+
calc_display = display.CalculatorDisplay(c)
14+
calc_display.create_gui(main)
15+
calc_display.frame.grid(row=0,sticky=tk.NSEW)
16+
17+
calc_buttons = button.CalculatorButtons(c)
18+
calc_buttons.create_gui(main)
19+
calc_buttons.frame.grid(row=1,sticky=tk.NSEW)
20+
21+
while True:
22+
calc_buttons.update()
23+
calc_display.update()
24+
main.update_idletasks()
25+
main.update()

0 commit comments

Comments
 (0)