Python 3.x 如何使用pygame将变量的值显示为文本

Python 3.x 如何使用pygame将变量的值显示为文本,python-3.x,pygame,pygame-surface,Python 3.x,Pygame,Pygame Surface,我在pygame 1.9.4中使用Windows 10和Python 3.7 我的应用程序要求变量的值显示在pygame屏幕上 目前,我有: tnr = pygame.font.SysFont('Times New Roman', 30) text2 = tnr.render(timePriorities, False, (0, 0, 0)) screen.blit(text2,(0,0)) 这将输出此错误: TypeError: text must be a unicode or byte

我在pygame 1.9.4中使用Windows 10和Python 3.7

我的应用程序要求变量的值显示在pygame屏幕上

目前,我有:

tnr = pygame.font.SysFont('Times New Roman', 30)
text2 = tnr.render(timePriorities, False, (0, 0, 0))
screen.blit(text2,(0,0))
这将输出此错误:

TypeError: text must be a unicode or byte
有人知道如何解决这个问题吗

谢谢

哦,这是相关代码:

timePriorities = 1.1
timeRightTurn = 2.2
timeDeadEnd = 3.3
timeIntersection = 4.4
array = sorted([timePriorities,timeRightTurn,timeDeadEnd,timeIntersection])
arrayLength = len(array)
tnr = pygame.font.SysFont('Times New Roman', 30)
for i in range(0,arrayLength):
    if timePriorities == array[i]:
        str(timePriorities)
        text1 = tnr.render('Priorities', False, (0, 0, 0))
        text2 = tnr.render(timePriorities, False, (0, 0, 0))
    else:
        if timeRightTurn == array[i]:
            str(timeRightTurn)
            text3 = tnr.render('Right Turn', False, (0, 0, 0))
            text4 = tnr.render(timeRightTurn, False, (0, 0, 0))
        else:
            if timeDeadEnd == array[i]:
                str(timeDeadEnd)
                text5 = tnr.render('Dead End', False, (0, 0, 0))
                text6 = tnr.render(timeDeadEnd, False, (0, 0, 0))
            else:
                if timeIntersection == array[i]:
                    str(timeIntersection)
                    text7 = tnr.render('Intersection', False, (0, 0, 0))
                    text8 = tnr.render(timeIntersection, False, (0, 0, 0))
问题在于str(timePriorities)不会将timePriorities更改为字符串,而是将值作为字符串返回

您需要这样分配它:

timePriorities = str(timePriorities) 
或者,您可以在渲染中执行以下操作:

text2 = tnr.render(str(timePriorities), False, (0, 0, 0))

我试着把它作为浮点输入,但没有成功。我试着把它转换成字符串,但没有成功。你能把所有的代码都发布出来让我看看吗?你是哪一行出错的嗯。。。祝你好运,能读到2000行。。。但我会在中发布重要的行。问题是str(timePriorities)不会将timePriorities更改为字符串,而是将值作为字符串返回。您需要这样分配它:timePriorities=str(timePriorities),或者您可以在render中这样做:text2=tnr.render(str(timePriorities),False,(0,0,0))@user2379875您应该将其添加到答案中。