Skip to content

Commit 3d242c4

Browse files
committed
Allow dynamic changing of the button label
1 parent c73cab2 commit 3d242c4

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

adafruit_button.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +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
9999
self.body = self.fill = self.shadow = None
100100

101101
self.fill_color = _check_color(fill_color)
102102
self.outline_color = _check_color(outline_color)
103-
self.label_color = label_color
103+
self._label_color = label_color
104+
self._label_font = label_font
104105
# Selecting inverts the button colors!
105106
self.selected_fill = _check_color(selected_fill)
106107
self.selected_outline = _check_color(selected_outline)
@@ -132,25 +133,41 @@ 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-
self.label = Label(label_font, text=label)
139-
dims = self.label.bounding_box
140-
if dims[2] >= width or dims[3] >= height:
141-
raise RuntimeError("Button not large enough for 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
136+
self.label = label
149137

150138
# else: # ok just a bounding box
151139
# self.bodyshape = displayio.Shape(width, height)
152140
# self.group.append(self.bodyshape)
153141

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+
154171
@property
155172
def selected(self):
156173
"""Selected inverts the colors."""
@@ -168,13 +185,13 @@ def selected(self, value):
168185
else:
169186
new_fill = self.fill_color
170187
new_out = self.outline_color
171-
new_label = self.label_color
188+
new_label = self._label_color
172189
# update all relevant colros!
173190
if self.body is not None:
174191
self.body.fill = new_fill
175192
self.body.outline = new_out
176-
if self.label is not None:
177-
self.label.color = new_label
193+
if self._label is not None:
194+
self._label.color = new_label
178195

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

0 commit comments

Comments
 (0)