I am trying to update an older PyPortal-based project, and running into a set_caption issue. It used to be that there was a single label for the caption, and its text was set during PyPortal() construction, and then could be updated later using set_caption.
Here is an example project Daily UV Index PyPortal Display that calls pyportal.set_caption() on every update, which is the same basic pattern that I've been using:
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/9f6d3ea8f8b16d12c6fd3b89cd7ce5bbf0c015ec/PyPortal_UV_Index/code.py#L100-L102
Code with the bug
Today's implementation of PyPortal.set_caption always calls PortalBase.add_text(), and therefore it adds a new label every time:
|
def set_caption(self, caption_text, caption_position, caption_color): |
|
# pylint: disable=line-too-long |
|
"""A caption. Requires setting ``caption_font`` in init! |
|
|
|
:param caption_text: The text of the caption. |
|
:param caption_position: The position of the caption text. |
|
:param caption_color: The color of your caption text. Must be a hex value, e.g. |
|
``0x808000``. |
|
""" |
|
# pylint: enable=line-too-long |
|
if self._debug: |
|
print("Setting caption to", caption_text) |
|
|
|
if (not caption_text) or (not self._caption_font) or (not caption_position): |
|
return # nothing to do! |
|
|
|
index = self.add_text( |
|
text_position=caption_position, |
|
text_font=self._caption_font, |
|
text_color=caption_color, |
|
is_data=False, |
|
) |
|
self.set_text(caption_text, index) |
Here's a link to the add_text implementation
Old Implementation
Looking into the source history, prior to #99, the caption label was created if needed, otherwise it was updated:
|
def set_caption(self, caption_text, caption_position, caption_color): |
|
# pylint: disable=line-too-long |
|
"""A caption. Requires setting ``caption_font`` in init! |
|
|
|
:param caption_text: The text of the caption. |
|
:param caption_position: The position of the caption text. |
|
:param caption_color: The color of your caption text. Must be a hex value, e.g. |
|
``0x808000``. |
|
|
|
""" |
|
# pylint: enable=line-too-long |
|
if self._debug: |
|
print("Setting caption to", caption_text) |
|
|
|
if (not caption_text) or (not self._caption_font) or (not caption_position): |
|
return # nothing to do! |
|
|
|
if self._caption: |
|
self._caption._update_text( # pylint: disable=protected-access |
|
str(caption_text) |
|
) |
|
try: |
|
board.DISPLAY.refresh(target_frames_per_second=60) |
|
except AttributeError: |
|
board.DISPLAY.refresh_soon() |
|
board.DISPLAY.wait_for_frame() |
|
return |
|
|
|
self._caption = Label(self._caption_font, text=str(caption_text)) |
|
self._caption.x = caption_position[0] |
|
self._caption.y = caption_position[1] |
|
self._caption.color = caption_color |
|
self.splash.append(self._caption) |
This is the behavior I was used to, and is still how I'd expect it to work based on today's documentation
Workaround
I think I can workaround this by ignoring the PyPortal caption, and just managing the label myself?
Fix
I'm not sure how to approach fixing this. The interaction with PortalBase makes it a little complicated. I cloned the repo with the ambition of opening a PR, but I'm not especially familiar with how other folks are using PortalBase / PyPortal.
- could PyPortal simply remember the index of the caption? if so, how does it move the label / change the color if needed?
- should there be a way to remove labels in PortalBase, which PyPortal could use to remove / re-create the caption when needed?
- would remembering the caption label index be fragile - and prone to break if/when the fetched data needs more / fewer labels?
- should the Adafruit_Learning_System_Guides code be updated to use PyPortal class /
set_caption differently?
I am trying to update an older PyPortal-based project, and running into a
set_captionissue. It used to be that there was a singlelabelfor the caption, and its text was set duringPyPortal()construction, and then could be updated later usingset_caption.Here is an example project Daily UV Index PyPortal Display that calls
pyportal.set_caption()on every update, which is the same basic pattern that I've been using:https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/9f6d3ea8f8b16d12c6fd3b89cd7ce5bbf0c015ec/PyPortal_UV_Index/code.py#L100-L102
Code with the bug
Today's implementation of
PyPortal.set_captionalways callsPortalBase.add_text(), and therefore it adds a new label every time:Adafruit_CircuitPython_PyPortal/adafruit_pyportal/__init__.py
Lines 274 to 296 in de0b008
Here's a link to the add_text implementation
Old Implementation
Looking into the source history, prior to #99, the caption label was created if needed, otherwise it was updated:
Adafruit_CircuitPython_PyPortal/adafruit_pyportal.py
Lines 586 to 618 in 88358c0
This is the behavior I was used to, and is still how I'd expect it to work based on today's documentation
Workaround
I think I can workaround this by ignoring the PyPortal caption, and just managing the label myself?
Fix
I'm not sure how to approach fixing this. The interaction with PortalBase makes it a little complicated. I cloned the repo with the ambition of opening a PR, but I'm not especially familiar with how other folks are using PortalBase / PyPortal.
set_captiondifferently?