-
Notifications
You must be signed in to change notification settings - Fork 22
save color even if text doesn't exist yet for index #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
forgot to mention. Resolves #43 |
I'm don't think this will work. The reason set_text() needs to be called first is because it creates all the new variables in the various lists. See https://github.com/adafruit/Adafruit_CircuitPython_MatrixPortal/blob/master/adafruit_matrixportal/matrixportal.py#L193 A better way to do this may be to just raise an error that the index doesn't exist and suggest the user calls set_text() first. |
Whoops, I meant the error should say to call |
It looks like it needs to call I changed the library to raise an error if the user specifies an index that doesn't exist yet: if self._text[index]:
color = self.html_color_convert(color)
self._text_color[index] = color
self._text[index].color = color
else:
raise IndexError(f"index {index} is out of bounds. Please call add_text() first.") And then tried this code: matrixportal = MatrixPortal()
matrixportal.add_text(
text_font=terminalio.FONT,
text_position=(0, (matrixportal.graphics.display.height // 2) - 1),
#text_color=0x3d1f5c,
text_transform=None,
)
# matrixportal.set_text(" AWAY",0) # this allows it to work
matrixportal.set_text_color(0xFF0000,0) but I get the index error being raised: I will tweak the wording to note that you need to call add_text() and set_text() first. |
… order to call the functions.
The latest commits undo the change to save the color and raise an exception with instructions for the user. With the latest version the code from above will no longer show the red label, but does now raise an error instruction to the user how to correct it. |
Ok, I think I understand better. The issue is that |
I tried like this: if 0 <= index <= len(self._text):
color = self.html_color_convert(color)
self._text_color[index] = color
self._text[index].color = color # <- line 260
else:
raise IndexError(
"index {} is out of bounds. Please call add_text() and set_text() first.".format(
index
)
) but now I get this error from the example above:
|
It's because the Text object is set to None until text is written to it. This should fix that:
|
Thank you. The newest commit implements those changes. I tested successfully on Matrix Portal with the above example code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great. Thank you.
Updating https://github.com/adafruit/Adafruit_CircuitPython_MatrixPortal to 1.9.1 from 1.9.0: > Merge pull request adafruit/Adafruit_CircuitPython_MatrixPortal#44 from FoamyGuy/text_coloring > Merge pull request adafruit/Adafruit_CircuitPython_MatrixPortal#40 from FoamyGuy/fix_readme_example_import
This code from the linked forum post makes red text using this version of the library:
It can still raise an exception if you try to use 1 or any larger index that doesn't exist yet. But it does seem to work correctly for this most basic use case.
Maybe it should check the length of
self._text_color
against the requested index and ignore values that are too large?