adafruit / Adafruit_CircuitPython_PortalBase

Base Library for the Portal-style libraries.
MIT License
18 stars 17 forks source link

Add example showing how to remove text? #61

Closed caternuson closed 2 years ago

caternuson commented 2 years ago

Seems to get asked a lot. How to remove a text label added with magtag.add_text()?

evaherrada commented 2 years ago

I think the easiest way is just to set the text to "". Looking to see if there's a way to actually remove it since that isn't an ideal solution.

evaherrada commented 2 years ago

@makermelissa is there a better way than magtag.set_text("") ?

makermelissa commented 2 years ago

I don't think there's a way to actually remove it at this point. I can't remember if magtag.set_text(None) works too.

wryfi commented 2 years ago

Setting the text to a null value doesn't remove the layout elements, so that doesn't really help much if you want to cycle between screens that have different font sizes and text positions.

I guess in theory dropping down to the display_text library is the way to work around this limitation?

makermelissa commented 2 years ago

I'm working on the library today, so I can look at what it would take to add this feature. My only hesitation with new features is adding too many can use up too much memory.

belthesar commented 2 years ago

magtag.add_text() seems to return the index of the text element. Perhaps being able to specify index on creation would allow you to overwrite text elements per screen. Usage would be something like

def page1():
    page1_text_label1 = magtag.add_text(index=0, ...)

def page2():
    page2_text_label1 = magtag.add_text(index=0, ...)
makermelissa commented 2 years ago

So I was looking into this and this could get really complicated quickly. We could have it so we pass in the index we want removed, but this would shuffle all of the other indices after it, which could make for some really off behaviors. Especially if users are expecting the index they received back from add_text to set the text or remove that item. I am thinking it would be better to not actually remove the text. My recommendation would be to just set the text to empty unless you have a compelling reason to do it a different way.

wryfi commented 2 years ago

I've managed to work around the issue well enough, but the behavior isn't very well documented, and the available solutions don't play very nice with some of the other methods on the object, like fetch() for example.

What I have done is to use a setup function that creates all the text boxes that my application needs, and then individual functions insert text (or empty strings) into them as needed.

It took me quite awhile to figure this out, however, it doesn't feel like a very elegant solution, it's annoying to keep track of which things get inserted into which boxes, and I don't know how far it would scale (e.g. how many text boxes the system can handle).

@makermelissa what about a method to remove all text boxes and start with a clean slate? Then at least I could write an isolated function for each screen.

makermelissa commented 2 years ago

@makermelissa what about a method to remove all text boxes and start with a clean slate? Then at least I could write an isolated function for each screen.

Now that sounds like a workable solution.