简体   繁体   中英

When executing a pyinstaller .exe file, pygame raises an error

I'm making a little game using mainly Tkinter, but for sounds I used pygame.

I've been trying to make an.exe file but it gives me an error when I execute.

pygame.init()

self.sound_player_shot = pygame.mixer.Sound('sounds/shot.wav')
self.sound_gameover = pygame.mixer.Sound('sounds/gameover.wav')

pygame.mixer.music.load('sounds/soundtrack.mp3')
pygame.mixer.music.play(-1)

That's the only codes from pygame that I use, and after creating the.exe file and copying all the data folders to the dist folder, when executing that error occurs:

Exception in Tkinter callback
Traceback (most recent call last):
  File "tkinter\__init__.py", line 1883, in __call__
  File "space_shooter.py", line 1292, in start_game
  File "space_shooter.py", line 115, in __init__
pygame.error

The sounds folder is in the same folder of space_shooter.py , and all my other smaller projects worked just by copying all the data folders into dist .

Thanks!

Have you checked whether the path you specify to load the sound files from are truly the paths you intend to use? An option might be to print(path) and start the exe from CMD to check it manually.

Without a more specific error message and knowing in which line the error actually originates, it is hard to give a more specific answer.

Furthermore I suggest you use the os module to get and set paths:

import os
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'relative/path/to/file/you/want.mp3')

PS: you might want to think about refactoring your "space_shooter.py" file, 1292 lines in one file sounds like a bit long in my opinion.

Including data with pyinstaller-compiled programs can be done at compile-time. There you can link a path to an actual file on your computer (relative or absolute) with an alias path you use in the program itself.

The documentation describes this process pretty well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM