Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

How do I change label text
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How do I change label text

by dougconran on Thu Jun 11, 2020 6:59 pm

I'm stumped! I'm displaying some text on a 1.44" TFT display. I want to be able to update that text without losing everything else eg a graphic. I cannot find any documentation and have tried everything I can think of by hacking around with groups but the best I can come up with is one text label changing at the expense of losing all other text/images on the screen.

My code is:-

Code: Select all | TOGGLE FULL SIZE
font = bitmap_font.load_font("/Rubik-Medium-17.bdf")
soc_txt = "73 %"
tmp_txt = "32 °C"
soh_txt = "99 %"
soc = label.Label(font, text=soc_txt, color=0x70ff70, x=70, y=22)
tmp = label.Label(font, text=tmp_txt, color=0x70ff70, x=70, y=64)
soh = label.Label(font, text=soh_txt, color=0x70ff70, x=70, y=104)

# Create a Group to hold the TileGrid
group = displayio.Group()
 
# Add the TileGrid to the Group
group.append(tile_grid)
group.append(soc)
group.append(tmp)
group.append(soh)

# Add the Group to the Display
display.show(group)

y = 0

while y < 50:
    soc_txt = str(100-y)+" %"
    print(soc_txt)
    soc = label.Label(font, text=soc_txt, color=0x70ff70, x=70, y=22)
    display.show(soc)  <== this changes soc but at the expense of everything else
    y += 1
    time.sleep(0.5)


How can I overwrite these text labels with new values?

Also, the font takes an age to load. I've cut it down as much as I can (although I'm not sure that that has worked). Is there any way I can speed up loading?

dougconran
 
Posts: 14
Joined: Wed Nov 08, 2017 8:25 am

Re: How do I change label text

by dastels on Thu Jun 11, 2020 7:15 pm

Something along the lines of:
Code: Select all | TOGGLE FULL SIZE
soc.text = "..."


Dave

dastels
 
Posts: 2829
Joined: Tue Oct 20, 2015 3:22 pm

Re: How do I change label text

by dougconran on Thu Jun 11, 2020 7:49 pm

Thanks, I'll try tomorrow as it is a bit late in the UK just now :).

dougconran
 
Posts: 14
Joined: Wed Nov 08, 2017 8:25 am

Re: How do I change label text

by dougconran on Fri Jun 12, 2020 5:44 am

Mmm, I tried that but it gave me a runtime error:-

Code: Select all | TOGGLE FULL SIZE
RuntimeError: Text length exceeds max_glyphs


My adjusted code is:-

Code: Select all | TOGGLE FULL SIZE
while y < 50:
    soc_txt = str(100-y)+" %"
    soc.text = soc_txt
    print(soc_txt)
    y += 1
    time.sleep(0.5)

dougconran
 
Posts: 14
Joined: Wed Nov 08, 2017 8:25 am

Re: How do I change label text

by dougconran on Fri Jun 12, 2020 8:18 am

SOLVED! The problem was that the initial value of the text was 5 characters but it was then being updated with a 5 character string - evidently the initial value sets the max_glyphs value which can't, then, be exceeded. The solution is to make the initial text length as long as it will ever be.

dougconran
 
Posts: 14
Joined: Wed Nov 08, 2017 8:25 am

Re: How do I change label text

by dastels on Fri Jun 12, 2020 10:37 am

Ah right. That makes sense. I had been doing that but to be able to get the width for centering purposes.

Dave

dastels
 
Posts: 2829
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.