我使用 Python 和 Pygame 在 Pi 上显示天气 我有 2 个盒子,我试图在其中显示风速和风向 这是我的代码
pg.draw.rect(screen, (255,255,255), (643, 67, 85, 30), 2) # draw windspeed box
if skyData.status == sky.STATUS_OK:
ren = font.render("{}°C".format(skyData.tempnow), 1, pg.Color('black'), pg.Color(134,174,230))
else:
ren = font.render("", 1, pg.Color('black'), pg.Color(185,208,240))
screen.blit(ren, (658*HRES//1600, 84*VRES//900-ren.get_height()//2))
pg.draw.rect(screen, (255,255,255), (872, 67, 116, 30), 2) # draw wind direction box cardinal + Degrees
if forecastData.status == forecast.STATUS_OK:
ren = font.render("{} {}°".format(forecastData.wind_direction_cardinal, forecastData.wind_direction), 1, pg.Color('black'), pg.Color(134,174,230))
else:
ren = font.render("", 1, pg.Color('black'), pg.Color(134,174,230))
screen.blit(ren, (882*HRES//1600, 84*VRES//900-ren.get_height()//2))
效果很漂亮,但文本从未居中 如果风速低于 10 英里/小时,则文本位于右侧,如果方向为 N,而不是 NW 或 NNW,则文本位于左侧
我希望文本在指定的框中居中 这可能吗?
获取文本Surface的边界矩形,并将矩形的中心设置为框的中心。使用矩形来
blit
文本:
box = pygame.Rect(643, 67, 85, 30), 2)
pg.draw.rect(screen, (255,255,255), box , 2) # draw windspeed box
if skyData.status == sky.STATUS_OK:
ren = font.render("{}°C".format(skyData.tempnow), 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)
的dest参数也可以是一个矩形。