简体   繁体   English

执行 pyinstaller.exe 文件时,pygame 引发错误

[英]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.我正在制作一个主要使用 Tkinter 的小游戏,但对于声音我使用了 pygame。

I've been trying to make an.exe file but it gives me an error when I execute.我一直在尝试制作一个 .exe 文件,但是当我执行时它给了我一个错误。

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:这是我使用的唯一来自 pygame 的代码,在创建 .exe 文件并将所有数据文件夹复制到dist文件夹后,执行时出现该错误:

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 . sounds文件夹与space_shooter.py位于同一文件夹中,而我所有其他较小的项目只需将所有数据文件夹复制到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.一个选项可能是print(path)并从 CMD 启动 exe 以手动检查它。

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:此外,我建议您使用os模块来获取和设置路径:

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. PS:你可能想考虑重构你的“space_shooter.py”文件,我认为一个文件中的 1292 行听起来有点长。

Including data with pyinstaller-compiled programs can be done at compile-time.可以在编译时将数据包含在 pyinstaller 编译的程序中。 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. 文档很好地描述了这个过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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