Skip to content

Commit 8af076e

Browse files
authored
Merge pull request #3 from ladyada/master
Allow for changing label of button
2 parents d2413ed + 3d242c4 commit 8af076e

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

adafruit_button.py

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT,
9595
self._selected = False
9696
self.group = displayio.Group()
9797
self.name = name
98-
self.label = label
98+
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)
102-
self.label_color = label_color
103+
self._label_color = label_color
104+
self._label_font = label_font
103105
# Selecting inverts the button colors!
104106
self.selected_fill = _check_color(selected_fill)
105107
self.selected_outline = _check_color(selected_outline)
@@ -111,7 +113,6 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT,
111113
self.selected_outline = (~self.outline_color) & 0xFFFFFF
112114

113115
if outline_color or fill_color:
114-
self.body = self.shadow = None
115116
if style == Button.RECT:
116117
self.body = Rect(x, y, width, height,
117118
fill=self.fill_color, outline=self.outline_color)
@@ -132,43 +133,65 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT,
132133
self.group.append(self.shadow)
133134
self.group.append(self.body)
134135

135-
if label and (label_color is not None): # button with text label
136-
if not label_font:
137-
raise RuntimeError("Please provide label font")
138-
dims = label_font.text_bounding_box(label)
139-
if dims[2] >= width or dims[3] >= height:
140-
raise RuntimeError("Button not large enough for label")
141-
self.label = Label(label_font, text=label)
142-
self.label.x = x + (width - dims[2]) // 2
143-
self.label.y = y + height // 2
144-
self.label.color = label_color
145-
self.group.append(self.label)
146-
147-
if self.selected_label is None and label_color is not None:
148-
self.selected_label = (~label_color) & 0xFFFFFF
149-
# print(dims)
136+
self.label = label
150137

151138
# else: # ok just a bounding box
152139
# self.bodyshape = displayio.Shape(width, height)
153140
# self.group.append(self.bodyshape)
154141

142+
@property
143+
def label(self):
144+
"""The text label of the button"""
145+
return self._label.text
146+
147+
@label.setter
148+
def label(self, newtext):
149+
if self._label and (self.group[-1] == self._label):
150+
self.group.pop()
151+
152+
self._label = None
153+
if not newtext or (self._label_color is None): # no new text
154+
return # nothing to do!
155+
156+
if not self._label_font:
157+
raise RuntimeError("Please provide label font")
158+
self._label = Label(self._label_font, text=newtext)
159+
dims = self._label.bounding_box
160+
if dims[2] >= self.width or dims[3] >= self.height:
161+
raise RuntimeError("Button not large enough for label")
162+
self._label.x = self.x + (self.width - dims[2]) // 2
163+
self._label.y = self.y + self.height // 2
164+
self._label.color = self._label_color
165+
self.group.append(self._label)
166+
167+
if (self.selected_label is None) and (self._label_color is not None):
168+
self.selected_label = (~self._label_color) & 0xFFFFFF
169+
170+
155171
@property
156172
def selected(self):
157173
"""Selected inverts the colors."""
158174
return self._selected
159175

160176
@selected.setter
161177
def selected(self, value):
162-
if value != self._selected:
163-
self._selected = value
178+
if value == self._selected:
179+
return # bail now, nothing more to do
180+
self._selected = value
164181
if self._selected:
165-
self.body.fill = self.selected_fill
166-
self.body.outline = self.selected_outline
167-
self.label.color = self.selected_label
182+
new_fill = self.selected_fill
183+
new_out = self.selected_outline
184+
new_label = self.selected_label
168185
else:
169-
self.body.fill = self.fill_color
170-
self.body.outline = self.outline_color
171-
self.label.color = self.label_color
186+
new_fill = self.fill_color
187+
new_out = self.outline_color
188+
new_label = self._label_color
189+
# update all relevant colros!
190+
if self.body is not None:
191+
self.body.fill = new_fill
192+
self.body.outline = new_out
193+
if self._label is not None:
194+
self._label.color = new_label
172195

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

0 commit comments

Comments
 (0)