Skip to content

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

Merged
merged 5 commits into from
Nov 10, 2020

Conversation

FoamyGuy
Copy link
Contributor

@FoamyGuy FoamyGuy commented Nov 9, 2020

This code from the linked forum post makes red text using this version of the library:

import board
import terminalio
from adafruit_matrixportal.matrixportal import MatrixPortal
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_color(0xFF0000,0)
matrixportal.set_text(" AWAY",0)
while True:
    pass

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?

@FoamyGuy
Copy link
Contributor Author

FoamyGuy commented Nov 9, 2020

forgot to mention. Resolves #43

@makermelissa
Copy link
Collaborator

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.

@makermelissa
Copy link
Collaborator

Whoops, I meant the error should say to call add_text() first.

@FoamyGuy
Copy link
Contributor Author

FoamyGuy commented Nov 9, 2020

It looks like it needs to call add_text() and set_text() first before calling set_text_color().

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: IndexError: index 0 is out of bounds. Please call add_text() first.

I will tweak the wording to note that you need to call add_text() and set_text() first.

@FoamyGuy
Copy link
Contributor Author

FoamyGuy commented Nov 9, 2020

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.

@makermelissa
Copy link
Collaborator

Ok, I think I understand better. The issue is that if self._text[index]: evaluates to False if the string is empty, which is why set_text needed to be set first. I was trying to check if the index existed. Maybe we should change it to something more like if 0 <= index <= len(self._text):

@tannewt tannewt requested a review from makermelissa November 9, 2020 23:37
@FoamyGuy
Copy link
Contributor Author

FoamyGuy commented Nov 9, 2020

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:

  File "code.py", line 13, in <module>
  File "/lib/adafruit_matrixportal/matrixportal.py", line 260, in set_text_color
AttributeError: 'NoneType' object cannot assign attribute 'color'

@makermelissa
Copy link
Collaborator

It's because the Text object is set to None until text is written to it. This should fix that:

        if 0 <= index < len(self._text_color):
            self._text_color[index] = self.html_color_convert(color)
            if self._text[index] is not None:
                self._text[index].color = self._text_color[index]
        else:
            raise IndexError(
                "index {} is out of bounds. Please call add_text() and set_text() first.".format(
                    index
                )
            )

@FoamyGuy
Copy link
Contributor Author

Thank you. The newest commit implements those changes. I tested successfully on Matrix Portal with the above example code.

Copy link
Collaborator

@makermelissa makermelissa left a 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.

@makermelissa makermelissa merged commit b4253b4 into adafruit:master Nov 10, 2020
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Nov 11, 2020
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants