Can anyone suggest how I can amend this code to print the string in red if it’s a negative value (contains “-”) or in black if it’s positive (no “-”).
I tried to set a variable ‘colour’ as ‘1’ or ‘2’ and then use that variable name in the inkyphat.text lines but it doesn’t seem to work.
Thanks.
import inkyphat
from PIL import ImageFont
mystr1 ="10,000"
mystr2 ="-10,000"
font_file = inkyphat.fonts.ocraextended
font_size = 32
top = 0
left = 0
offset_left = 0
inkyphat.rectangle([(0, 0), (212, 104)], fill=inkyphat.WHITE, outline=None)
font = inkyphat.ImageFont.truetype(font_file, font_size)
width, height = font.getsize(mystr1)
inkyphat.text((0, top), mystr1, 1, font=font) #Prints in red
top += height + 1
left = max(left, offset_left + width)
font = inkyphat.ImageFont.truetype(font_file, font_size)
width, height = font.getsize(mystr2)
inkyphat.text((0, top), mystr2, 2, font=font) #Prints in black
top += height + 2
left = max(left, offset_left + width)
inkyphat.set_rotation(180)
inkyphat.set_colour('red')
inkyphat.show()
2 posts - 2 participants