在 Python Pygame 中调用对象和传递变量不工作错误说变量未定义
Calling Objects and Passing Variables in Python Pygame Not Working Error Says Variable Not Defined
我对对象和传递在 Python 中的工作方式有点困惑。我想创建一个对象来调用 class 的函数,但我似乎无法传递 mainWindow 变量。我一直收到一条错误消息,说 mainWindow 尚未定义
几年前我在 TKinter 中编写了一个程序,并将所有内容都放在一个 main 方法中,并且在每个函数完成后它会调用下一个函数并传递变量。我想看看是否可以通过对象调用函数来做同样的事情。
import pygame
pygame.init
class PreviewWindow:
def __init__(self):
mainWindow = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Sprite Viewer')
def loadImage(self, mainWindow):
userImage = pygame.image.load('well.png')
imageSize = userImage.get_rect()
def drawImage(self, userImage, imageSize):
mainWindow.blit(userImage, imageSize)
pygame.display.flip()
previewObj = PreviewWindow
previewObj.loadImage(mainWindow)
previewObj.drawImage(mainWindow, userImage, imageSize)
我想了解如何在 classes 中调用函数,同时能够将变量和函数传递给所述函数。
这里发生了几件事。首先,您在 __init__
函数的范围内定义 mainWindow
。这意味着不能从函数外部引用它。在开始将 mainWindow 传递给 class 的方法之前,您一直在正确使用 OOP。相反,只需使用 __init__
!
已经定义的 mainWindow
您可以通过设置 self.mainWindow
来做到这一点。 self
使 属性 对象具体化。
import pygame
pygame.init
class PreviewWindow:
def __init__(self):
# initialize your mainWindow
self.mainWindow = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Sprite Viewer')
def loadImage(self, imageName):
self.userImage = pygame.image.load(imageName)
self.imageSize = userImage.get_rect()
def drawImage(self):
# use the mainWindow you initialized in __init__
self.mainWindow.blit(self.userImage, self.imageSize)
pygame.display.flip()
previewObj = PreviewWindow()
previewObj.loadImage('well.png')
previewObj.drawImage()
我对对象和传递在 Python 中的工作方式有点困惑。我想创建一个对象来调用 class 的函数,但我似乎无法传递 mainWindow 变量。我一直收到一条错误消息,说 mainWindow 尚未定义
几年前我在 TKinter 中编写了一个程序,并将所有内容都放在一个 main 方法中,并且在每个函数完成后它会调用下一个函数并传递变量。我想看看是否可以通过对象调用函数来做同样的事情。
import pygame
pygame.init
class PreviewWindow:
def __init__(self):
mainWindow = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Sprite Viewer')
def loadImage(self, mainWindow):
userImage = pygame.image.load('well.png')
imageSize = userImage.get_rect()
def drawImage(self, userImage, imageSize):
mainWindow.blit(userImage, imageSize)
pygame.display.flip()
previewObj = PreviewWindow
previewObj.loadImage(mainWindow)
previewObj.drawImage(mainWindow, userImage, imageSize)
我想了解如何在 classes 中调用函数,同时能够将变量和函数传递给所述函数。
这里发生了几件事。首先,您在 __init__
函数的范围内定义 mainWindow
。这意味着不能从函数外部引用它。在开始将 mainWindow 传递给 class 的方法之前,您一直在正确使用 OOP。相反,只需使用 __init__
!
mainWindow
您可以通过设置 self.mainWindow
来做到这一点。 self
使 属性 对象具体化。
import pygame
pygame.init
class PreviewWindow:
def __init__(self):
# initialize your mainWindow
self.mainWindow = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Sprite Viewer')
def loadImage(self, imageName):
self.userImage = pygame.image.load(imageName)
self.imageSize = userImage.get_rect()
def drawImage(self):
# use the mainWindow you initialized in __init__
self.mainWindow.blit(self.userImage, self.imageSize)
pygame.display.flip()
previewObj = PreviewWindow()
previewObj.loadImage('well.png')
previewObj.drawImage()