@@ -96,6 +96,7 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT,
96
96
self .group = displayio .Group ()
97
97
self .name = name
98
98
self .label = label
99
+ self .body = self .fill = self .shadow = None
99
100
100
101
self .fill_color = _check_color (fill_color )
101
102
self .outline_color = _check_color (outline_color )
@@ -111,7 +112,6 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT,
111
112
self .selected_outline = (~ self .outline_color ) & 0xFFFFFF
112
113
113
114
if outline_color or fill_color :
114
- self .body = self .shadow = None
115
115
if style == Button .RECT :
116
116
self .body = Rect (x , y , width , height ,
117
117
fill = self .fill_color , outline = self .outline_color )
@@ -135,18 +135,17 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT,
135
135
if label and (label_color is not None ): # button with text label
136
136
if not label_font :
137
137
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
139
140
if dims [2 ] >= width or dims [3 ] >= height :
140
141
raise RuntimeError ("Button not large enough for label" )
141
- self .label = Label (label_font , text = label )
142
142
self .label .x = x + (width - dims [2 ]) // 2
143
143
self .label .y = y + height // 2
144
144
self .label .color = label_color
145
145
self .group .append (self .label )
146
146
147
147
if self .selected_label is None and label_color is not None :
148
148
self .selected_label = (~ label_color ) & 0xFFFFFF
149
- # print(dims)
150
149
151
150
# else: # ok just a bounding box
152
151
# self.bodyshape = displayio.Shape(width, height)
@@ -159,16 +158,23 @@ def selected(self):
159
158
160
159
@selected .setter
161
160
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
164
164
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
168
168
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
172
178
173
179
def contains (self , point ):
174
180
"""Used to determine if a point is contained within a button. For example,
0 commit comments