1

I'm really new to Python and I have a display for my Pi 4 which uses Pygame for the display. I have a box with centred text and I'd like if possible to have the inside background of the box to be a different colour if possible I'd like the background of the box to be RGB (141,180,235) The main display is BACKGROUND_COLOR = pg.Color(42,117,198) which is what the inside of this box is.

Is this possible?

 box = pg.Rect(645, 75, 180, 30)
pg.draw.rect(screen, (255,255,255,), box , 1)  # draw windspeed box           
if skyData.status == sky.STATUS_OK: 
    ren = font.render("Wind Direction {}°".format(forecastData.angle), 1, pg.Color('black'), pg.Color(134,174,230))
else:
    ren = font.render("", 1, pg.Color('black'), pg.Color(185,208,240))
ren_rect = ren.get_rect(center = box.center)
screen.blit(ren, ren_rect)  
pg.draw.line(screen, pg.Color('black'), (644, 74), (825, 74)) #shade box
pg.draw.line(screen, pg.Color('black'), (644, 74), (644, 104))  #shade box

This is a BOX with CENTERED TEXT so just changing the background colour of the text will not work This is what happens if only this is one - colour different to show the change enter image description here

Lewis
  • 55
  • 6

1 Answers1

2

The 4th argument of render() is the background color. If you want to draw a box with a different background, just use a different background color. If you skip the the background argument, the background is transparent.
Use pygame.draw.rect to draw a rectangle with a certain color. Draw a text with a transparent background in its center:

text = ""
if skyData.status == sky.STATUS_OK: 
    text = "Wind Direction {}°".format(forecastData.angle)
ren = font.render(text, 1, pg.Color('black'))

box = pg.Rect(645, 75, 180, 30)
pg.draw.rect(screen, (141,180,235), box)
pg.draw.rect(screen, (255,255,255,), box, 1)

ren_rect = ren.get_rect(center = box.center)
screen.blit(ren, ren_rect) 

pg.draw.line(screen, pg.Color('black'), (644, 74), (825, 74)) #shade box
pg.draw.line(screen, pg.Color('black'), (644, 74), (644, 104))  #shade box

Rabbid76
  • 202,892
  • 27
  • 131
  • 174