[英]How to display a variable value on a button pygame?
基本上,我正在制作一個 COVID-19 機器人來跟蹤世界各地的所有病例和死亡人數。
def total_cases_counter():
# create the button
button = pygame.Rect(63, 183, 105, 40)
pygame.draw.rect(screen, [0, 0, 0], button)
button_font = pygame.font.Font('GOODDP__.TTF', 32)
button_text = button_font.render(total_cases, True, (255, 255, 255))
screen.blit(button_text, (205, 410))
這是我的 function 來顯示總病例計數器,我為其創建了一個按鈕,然后嘗試顯示我作為變量從 API 更新的總病例數。 我遇到的問題是我不知道如何在按鈕上顯示變量值(全球總病例數)。 我嘗試使用渲染 function 但第一個參數必須是字符串,這意味着我無法顯示我的變量。
我認為問題在於您的變量“total_cases”是 integer 對嗎? 如果是這樣,那就試試吧:
def total_cases_counter(): # create the button button = pygame.Rect(63, 183, 105, 40) pygame.draw.rect(screen, [0, 0, 0], button) button_font = pygame.font.Font('GOODDP__.TTF', 32) button_text = button_font.render(str(total_cases), True, (255, 255, 255)) screen.blit(button_text, (205, 410))
如您所見,我使用“str(total_cases)”更改了變量的類型。
PS total_cases 是全局變量嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.