Skip to content

Commit 6ff2955

Browse files
committed
fix selection code and label print
1 parent d2413ed commit 6ff2955

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

adafruit_button.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT,
9696
self.group = displayio.Group()
9797
self.name = name
9898
self.label = label
99+
self.body = self.fill = self.shadow = None
99100

100101
self.fill_color = _check_color(fill_color)
101102
self.outline_color = _check_color(outline_color)
@@ -111,7 +112,6 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT,
111112
self.selected_outline = (~self.outline_color) & 0xFFFFFF
112113

113114
if outline_color or fill_color:
114-
self.body = self.shadow = None
115115
if style == Button.RECT:
116116
self.body = Rect(x, y, width, height,
117117
fill=self.fill_color, outline=self.outline_color)
@@ -135,18 +135,17 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT,
135135
if label and (label_color is not None): # button with text label
136136
if not label_font:
137137
raise RuntimeError("Please provide label font")
138-
dims = label_font.text_bounding_box(label)
138+
self.label = Label(label_font, text=label)
139+
dims = self.label.bounding_box
139140
if dims[2] >= width or dims[3] >= height:
140141
raise RuntimeError("Button not large enough for label")
141-
self.label = Label(label_font, text=label)
142142
self.label.x = x + (width - dims[2]) // 2
143143
self.label.y = y + height // 2
144144
self.label.color = label_color
145145
self.group.append(self.label)
146146

147147
if self.selected_label is None and label_color is not None:
148148
self.selected_label = (~label_color) & 0xFFFFFF
149-
# print(dims)
150149

151150
# else: # ok just a bounding box
152151
# self.bodyshape = displayio.Shape(width, height)
@@ -159,16 +158,23 @@ def selected(self):
159158

160159
@selected.setter
161160
def selected(self, value):
162-
if value != self._selected:
163-
self._selected = value
161+
if value == self._selected:
162+
return # bail now, nothing more to do
163+
self._selected = value
164164
if self._selected:
165-
self.body.fill = self.selected_fill
166-
self.body.outline = self.selected_outline
167-
self.label.color = self.selected_label
165+
new_fill = self.selected_fill
166+
new_out = self.selected_outline
167+
new_label = self.selected_label
168168
else:
169-
self.body.fill = self.fill_color
170-
self.body.outline = self.outline_color
171-
self.label.color = self.label_color
169+
new_fill = self.fill_color
170+
new_out = self.outline_color
171+
new_label = self.label_color
172+
173+
if self.body is not None:
174+
self.body.fill = new_fill
175+
self.body.outline = new_out
176+
if self.label is not None:
177+
self.label.color = new_label
172178

173179
def contains(self, point):
174180
"""Used to determine if a point is contained within a button. For example,

0 commit comments

Comments
 (0)