Question

I've run into issues with pygame trying to load images when I imported my mainmenu file. It's successful when I run it on its own, but when I import from the Main file I get an error as it tries to load the image:

"pygame.error: Couldn't open Resources/BKg.png"

This is my original loading code in mainmenu:

bkg = pygame.image.load("Resources/BKg.png").convert_alpha()

but then I modified it to the below, thinking the package layout might have been the issue, didn't work though.

bkg = pygame.image.load(os.path.join("Resources","BKg.png")).convert_alpha()

This is my file structure:

Project/

 Main.py ---------  start here, import Package.mainmenu, load Bkg.png FAILURE
 Package/
         mainmenu.py ------- start here, load Bkg.png SUCCESS
         Resources/
                   Bkg.png
Was it helpful?

Solution

if you are loading from Main.py you need to do it like this:

bkg = pygame.image.load("Package/Resources/BKg.png").convert_alpha()

or

bkg = pygame.image.load(os.path.join("Package","Resources","BKg.png")).convert_alpha()

because yopure structure says Resources is in Package

just because you imported from the Package directory doesn't mean you can load images from it

OTHER TIPS

Try this one:

bkg = pygame.image.load("Package/Resources/BKg.png").convert_alpha()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top