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.
displaysurfonly exists for the scope of the function.returnit from youropen()method.window.open()to return displaysurf. Of course this means wherever you want to get this value, you'll need a valid instance ofwindow.