首页 > 解决方案 > 是否可以让这个带有居中文本的盒子内部有不同的颜色?

问题描述

我对 Python 很陌生,我的 Pi 4 有一个显示器,它使用 Pygame 进行显示。我有一个带有居中文本的框,如果可能的话,我希望框的内部背景为不同的颜色我希望框的背景为 RGB (141,180,235) 主显示是背景颜色 = pg.Color(42,117,198) 这就是这个盒子里面的东西。

这可能吗?

 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

这是一个带有 CENTERED TEXT 的 BOX,所以仅仅改变文本的背景颜色是行不通的 在此处输入图像描述

标签: pythonpython-3.xpygamebackground-colorcentering

解决方案


的第四个参数render()是背景颜色。如果要绘制具有不同背景的框,只需使用不同的背景颜色即可。如果跳过背景参数,则背景是透明的。
用于pygame.draw.rect绘制具有特定颜色的矩形。在其中心绘制一个具有透明背景的文本:

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


推荐阅读