diff --git a/adafruit_button.py b/adafruit_button.py index bfb119e..0a7bef4 100755 --- a/adafruit_button.py +++ b/adafruit_button.py @@ -100,14 +100,13 @@ def __init__( selected_outline=None, selected_label=None ): - super().__init__() + super().__init__(x=x, y=y) self.x = x self.y = y self.width = width self.height = height self._font = label_font self._selected = False - self.group = displayio.Group() self.name = name self._label = label self.body = self.fill = self.shadow = None @@ -129,8 +128,8 @@ def __init__( if (outline_color is not None) or (fill_color is not None): if style == Button.RECT: self.body = Rect( - x, - y, + 0, + 0, width, height, fill=self.fill_color, @@ -138,8 +137,8 @@ def __init__( ) elif style == Button.ROUNDRECT: self.body = RoundRect( - x, - y, + 0, + 0, width, height, r=10, @@ -147,12 +146,10 @@ def __init__( outline=self.outline_color, ) elif style == Button.SHADOWRECT: - self.shadow = Rect( - x + 2, y + 2, width - 2, height - 2, fill=outline_color - ) + self.shadow = Rect(2, 2, width - 2, height - 2, fill=outline_color) self.body = Rect( - x, - y, + 0, + 0, width - 2, height - 2, fill=self.fill_color, @@ -160,11 +157,11 @@ def __init__( ) elif style == Button.SHADOWROUNDRECT: self.shadow = RoundRect( - x + 2, y + 2, width - 2, height - 2, r=10, fill=self.outline_color + 2, 2, width - 2, height - 2, r=10, fill=self.outline_color ) self.body = RoundRect( - x, - y, + 0, + 0, width - 2, height - 2, r=10, @@ -172,8 +169,8 @@ def __init__( outline=self.outline_color, ) if self.shadow: - self.group.append(self.shadow) - self.group.append(self.body) + self.append(self.shadow) + self.append(self.body) self.label = label @@ -184,8 +181,8 @@ def label(self): @label.setter def label(self, newtext): - if self._label and self.group and (self.group[-1] == self._label): - self.group.pop() + if self._label and self and (self[-1] == self._label): + self.pop() self._label = None if not newtext or (self._label_color is None): # no new text @@ -197,10 +194,10 @@ def label(self, newtext): dims = self._label.bounding_box if dims[2] >= self.width or dims[3] >= self.height: raise RuntimeError("Button not large enough for label") - self._label.x = self.x + (self.width - dims[2]) // 2 - self._label.y = self.y + self.height // 2 + self._label.x = (self.width - dims[2]) // 2 + self._label.y = self.height // 2 self._label.color = self._label_color - self.group.append(self._label) + self.append(self._label) if (self.selected_label is None) and (self._label_color is not None): self.selected_label = (~self._label_color) & 0xFFFFFF @@ -230,6 +227,16 @@ def selected(self, value): if self._label is not None: self._label.color = new_label + @property + def group(self): + """Return self for compatibility with old API.""" + print( + "Warning: The group property is being deprecated. " + "User code should be updated to add the Button directly to the " + "Display or other Groups." + ) + return self + def contains(self, point): """Used to determine if a point is contained within a button. For example, ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for diff --git a/examples/display_button_customfont.py b/examples/display_button_customfont.py index 1c6d5aa..bce7679 100644 --- a/examples/display_button_customfont.py +++ b/examples/display_button_customfont.py @@ -139,7 +139,7 @@ buttons.append(button_6) for b in buttons: - splash.append(b.group) + splash.append(b) while True: p = ts.touch_point diff --git a/examples/display_button_simpletest.py b/examples/display_button_simpletest.py index 4db6e28..0fe5a73 100644 --- a/examples/display_button_simpletest.py +++ b/examples/display_button_simpletest.py @@ -45,7 +45,7 @@ ) # Add button to the display context -splash.append(button.group) +splash.append(button) # Loop and look for touches while True: diff --git a/examples/display_button_soundboard.py b/examples/display_button_soundboard.py index de2ec36..06537d2 100644 --- a/examples/display_button_soundboard.py +++ b/examples/display_button_soundboard.py @@ -40,7 +40,7 @@ label_color=None, name=spot["file"], ) - pyportal.splash.append(button.group) + pyportal.splash.append(button) buttons.append(button) last_pressed = None