Skip to content

Commit 0e06cd4

Browse files
authored
Merge pull request #759 from makermelissa/master
Added operation button highlighting for Calculator
2 parents 46fb70d + 59958c2 commit 0e06cd4

2 files changed

Lines changed: 52 additions & 28 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: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@
4646
font = bitmap_font.load_font("/fonts/Arial-12.bdf")
4747
buttons = []
4848

49-
# Some button placement functions
49+
# Some button functions
5050
def button_grid(row, col):
5151
return Coords(BUTTON_MARGIN * (row + 1) + BUTTON_WIDTH * row + 20,
5252
BUTTON_MARGIN * (col + 1) + BUTTON_HEIGHT * col + 40)
5353

54-
def make_button(row, col, label, width=1, color=WHITE, text_color=BLACK):
54+
def add_button(row, col, label, width=1, color=WHITE, text_color=BLACK):
5555
pos = button_grid(row, col)
5656
new_button = Button(x=pos.x, y=pos.y,
5757
width=BUTTON_WIDTH * width + BUTTON_MARGIN * (width - 1),
@@ -60,29 +60,36 @@ def make_button(row, col, label, width=1, color=WHITE, text_color=BLACK):
6060
buttons.append(new_button)
6161
return new_button
6262

63+
def find_button(label):
64+
result = None
65+
for _, btn in enumerate(buttons):
66+
if btn.label == label:
67+
result = btn
68+
return result
69+
6370
border = Rect(20, 8, 280, 35, fill=WHITE, outline=BLACK, stroke=2)
6471
calc_display = Label(font, text="0", color=BLACK, max_glyphs=MAX_DIGITS)
6572
calc_display.y = 25
6673

67-
clear_button = make_button(0, 0, "AC")
68-
make_button(1, 0, "+/-")
69-
make_button(2, 0, "%")
70-
make_button(3, 0, "/", 1, ORANGE, WHITE)
71-
make_button(0, 1, "7")
72-
make_button(1, 1, "8")
73-
make_button(2, 1, "9")
74-
make_button(3, 1, "x", 1, ORANGE, WHITE)
75-
make_button(0, 2, "4")
76-
make_button(1, 2, "5")
77-
make_button(2, 2, "6")
78-
make_button(3, 2, "-", 1, ORANGE, WHITE)
79-
make_button(0, 3, "1")
80-
make_button(1, 3, "2")
81-
make_button(2, 3, "3")
82-
make_button(3, 3, "+", 1, ORANGE, WHITE)
83-
make_button(0, 4, "0", 2)
84-
make_button(2, 4, ".")
85-
make_button(3, 4, "=", 1, ORANGE, WHITE)
74+
clear_button = add_button(0, 0, "AC")
75+
add_button(1, 0, "+/-")
76+
add_button(2, 0, "%")
77+
add_button(3, 0, "/", 1, ORANGE, WHITE)
78+
add_button(0, 1, "7")
79+
add_button(1, 1, "8")
80+
add_button(2, 1, "9")
81+
add_button(3, 1, "x", 1, ORANGE, WHITE)
82+
add_button(0, 2, "4")
83+
add_button(1, 2, "5")
84+
add_button(2, 2, "6")
85+
add_button(3, 2, "-", 1, ORANGE, WHITE)
86+
add_button(0, 3, "1")
87+
add_button(1, 3, "2")
88+
add_button(2, 3, "3")
89+
add_button(3, 3, "+", 1, ORANGE, WHITE)
90+
add_button(0, 4, "0", 2)
91+
add_button(2, 4, ".")
92+
add_button(3, 4, "=", 1, ORANGE, WHITE)
8693

8794
# Add the display and buttons to the main calc group
8895
calc_group.append(border)
@@ -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:
99-
for i, b in enumerate(buttons):
106+
# Button Down Events
107+
for _, 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)