@@ -95,12 +95,13 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT,
95
95
self ._selected = False
96
96
self .group = displayio .Group ()
97
97
self .name = name
98
- self .label = label
98
+ self ._label = label
99
99
self .body = self .fill = self .shadow = None
100
100
101
101
self .fill_color = _check_color (fill_color )
102
102
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
104
105
# Selecting inverts the button colors!
105
106
self .selected_fill = _check_color (selected_fill )
106
107
self .selected_outline = _check_color (selected_outline )
@@ -132,25 +133,41 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT,
132
133
self .group .append (self .shadow )
133
134
self .group .append (self .body )
134
135
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
149
137
150
138
# else: # ok just a bounding box
151
139
# self.bodyshape = displayio.Shape(width, height)
152
140
# self.group.append(self.bodyshape)
153
141
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
+
154
171
@property
155
172
def selected (self ):
156
173
"""Selected inverts the colors."""
@@ -168,13 +185,13 @@ def selected(self, value):
168
185
else :
169
186
new_fill = self .fill_color
170
187
new_out = self .outline_color
171
- new_label = self .label_color
188
+ new_label = self ._label_color
172
189
# update all relevant colros!
173
190
if self .body is not None :
174
191
self .body .fill = new_fill
175
192
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
178
195
179
196
def contains (self , point ):
180
197
"""Used to determine if a point is contained within a button. For example,
0 commit comments