问题
how can i get a variable from another class like that
white=(255,255,255)
class window:
def open(a,b,c):
displaysurf=pygame.display.set_mode(a)
pygame.display.set_caption(b)
displaysurf.fill(c)
def close():
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
class img():
def show(image,position):
pygame.image.load(image)
displaysurf.blit(image,position)
i want to get the displaysurf
variable from window.open()
function
回答1:
You absolutely can access variables in other classes. You just need to add the class name and function name before the variable name, with each separated by a period - example:
CLASSNAME.FunctionName.variableName = "abc"
This will allow you to access the variables from other functions without having to make them global, placed before the initialization or use return statements. Here's a basic example first:
class CLASS1:
def testingFunc():
CLASS1.testingFunc.thisValue = 123
class CLASS2:
def createValue():
CLASS1.testingFunc()
print(CLASS1.testingFunc.thisValue)
CLASS2.createValue()
Here's what your code might need to look like to make this work in your specific context:
white=(255,255,255)
class window:
def open(a,b,c):
window.open.displaysurf=pygame.display.set_mode(a)
pygame.display.set_caption(b)
window.open.displaysurf.fill(c)
def close():
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
class img():
def show(image,position):
pygame.image.load(image)
window.open.displaysurf.blit(image,position)
You may want to write another function, inside of which you have calls to your window class and the img class, that way you don't unnecessarily create an extra instance of something. I'm not familiar with pygame, nor do I know what this code is intended to do, but I know this method works because I use it myself all the time.
回答2:
It is not possible to access a variable assigned in a function unless you use return. However, if the variable is assigned in the class instance, then you can access it with certain limitations.
The window
class would therefore take the form of
class window(object):
# initialize class variable
def __init__(self, a, b, c):
self.display_surf = pygame.display.set_mode(a)
pygame.display.set_caption(b)
self.display_surf.fill(c)
def close(self):
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
The open()
function is embedded in the __init__()
function because the display_surf
variable is only assigned with the parameter a
and at that point it would make sense to set the other attributes as well.
Now to access the variable, you first have to initialize the class. You can do so during the creation of the img
class or after. It is best to do so after for more flexibility. The img
class would therefore need an extra parameter to access the variable.
class img(object):
def show(win, img, pos):
pygame.image.load(img)
win.display_surf.blit(img, pos)
# set up and access
win = window( # a, b, c)
img.show(win, # img, pos)
The reason I said limitations is because the window
class must be initialized first before using the variable and any function that wants to use it must have it as one of the parameters.
If you'd rather not want to include it as a parameter for a function, you'd make the win
variable a global variable. However, it is advised not to do so because the variable could be tampered with outside the intended course and mess up the entire program.
来源:https://stackoverflow.com/questions/44578240/how-to-get-variable-from-another-class-python