如何在Python中修复'Name is not defined'错误

2024-04-26 00:13:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到的错误名称没有定义它显示了这:在

Traceback (most recent call last):File "C:/Users/Shashank Kumar/AppData/Local/Programs/Python/Python37/ssss.py", line 1084, in changeBackgroundAnimation DISPLAYSURF.fill(bgcolor) NameError: name 'bgcolor' is not defined

def changeBackgroundAnimation(animationSpeed=40):
    global bgcolor
    newBgColor=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
    newBgSurf=pygame.Surface((WINDOWWIDTH,WINDOWHEIGHT))
    newBgSurf=newBgSurf.convert_alpha()
    r,g,b=newBgColor
    for alpha in range(0,255,animationSpeed):
        checkForQuit()
        DISPLAYSURF.fill(bgcolor)
        newBgSurf.fill((r,g,b,alpha))
        DISPLAYSURF.blit(newBgSurf,(0,0))
        drawButtons()
        pygame.display.update()
        FPSCLOCK.tick(FPS)
    bgcolor=newBgColor

NameError: name 'bgcolor' is not defined


Tags: nameinalphaisnotrandomfillrandint
1条回答
网友
1楼 · 发布于 2024-04-26 00:13:30

global bgcolor不定义、声明或以其他方式创建变量。它只是声明应该使用一个全局变量,而不是一个局部变量。您仍然需要确保在DISPLAYSURF.fill(bgcolor)中第一次使用之前定义了它。具体来说,您需要在第一次调用changeBackgroundAnimation之前为变量赋值。在

相关问题 更多 >