Skip to content

Commit 59e48e8

Browse files
committed
Added operation button highlighting
1 parent e54f970 commit 59e48e8

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

PyPortal_Calculator/calculator.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ def __init__(self, calc_display, clear_button, label_offset):
2323
self._operand = None
2424
self._all_clear()
2525

26+
def get_current_operator(self):
27+
operator = self._operator
28+
if operator == "*":
29+
operator = "x"
30+
return operator
31+
2632
def _all_clear(self):
2733
self._accumulator = "0"
2834
self._operator = None
@@ -36,6 +42,7 @@ def _clear_entry(self):
3642
self._set_text("0")
3743

3844
def _set_button_ce(self, entry_only):
45+
self._clear_button.selected = False
3946
if entry_only:
4047
self._clear_button.label = "CE"
4148
else:

PyPortal_Calculator/code.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ def make_button(row, col, label, width=1, color=WHITE, text_color=BLACK):
8484
make_button(2, 4, ".")
8585
make_button(3, 4, "=", 1, ORANGE, WHITE)
8686

87+
def find_button(label):
88+
result = None
89+
for i, b in enumerate(buttons):
90+
if b.label == label:
91+
result = b
92+
return result
93+
8794
# Add the display and buttons to the main calc group
8895
calc_group.append(border)
8996
calc_group.append(calc_display)
@@ -96,15 +103,25 @@ def make_button(row, col, label, width=1, color=WHITE, text_color=BLACK):
96103
while True:
97104
point = ts.touch_point
98105
if point is not None:
106+
# Button Down Events
99107
for i, b in enumerate(buttons):
100108
if b.contains(point) and button == "":
101109
b.selected = True
102110
button = b.label
103-
time.sleep(0.1)
111+
elif button != "":
112+
# Button Up Events
113+
last_op = calculator.get_current_operator()
114+
op_button = find_button(last_op)
115+
# Deselect the last operation when certain buttons are pressed
116+
if op_button is not None:
117+
if button in ('=', 'AC', 'CE'):
118+
op_button.selected = False
119+
elif button in ('+', '-', 'x', '/') and button != last_op:
120+
op_button.selected = False
121+
calculator.add_input(button)
122+
b = find_button(button)
123+
if b is not None:
124+
if button not in ('+', '-', 'x', '/') or button != calculator.get_current_operator():
104125
b.selected = False
105-
break
106-
else:
107-
if button != "":
108-
calculator.add_input(button)
109-
button = ""
126+
button = ""
110127
time.sleep(0.05)

0 commit comments

Comments
 (0)