问题创建可执行pygame的 - pyinstaller

问题描述 投票:0回答:1

我最近开始上的游戏作品为我女儿学习的话帮助。我顺动,并决定建立可执行文件(我将与许多其他游戏做到了这一点)。这场比赛是有点不同,由于声音和音乐。我已经试过所有我能想到的,都搜索我能想到的等等。这是错误CMD报告,错误指的是声音文件。我曾尝试将直接与--add-data我试图把可执行文件在同一目录中的声音文件(这应该没有必要,因为它应该已经将其捆绑)的文件。运行该脚本绝对没问题,否则(从CMD等)的任何想法?

C:\Users\launc\Desktop\Coding\Games\g_sight_words\dist>sight_words.exe pygame 1.9.4 Hello from the pygame community. https://www.pygame.org/contribute.html Traceback (most recent call last): File "sight_words.py", line 5, in <module> File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "c:\users\launc\appdata\local\programs\python\python37-32\lib\site- packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module exec(bytecode, module.__dict__) File "settings.py", line 21, in <module> pygame.error: Unable to open file 'the.wav' [9892] Failed to execute script sight_words

audio pygame executable pyinstaller
1个回答
0
投票

什么是您的.spec文件是什么样子?这里的PyInstaller docs on adding data files

基本上你需要添加类似:

a = Analysis(...
 datas=[ ('the.wav', '.') ],
 ...
 )

这将使你的声音文件(“the.wav”)到您编译应用程序的根目录(第二个参数,“”)

然后在你的应用程序,你可以检查,如果你是从源代码或作为编译的可执行文件运行。我使用一个辅助功能:

def my_path(path_name):
    """Return the appropriate path for data files based on execution context"""
    if getattr( sys, 'frozen', False ):
        # running in a bundle
        return(os.path.join(sys._MEIPASS, path_name))
    else:
        # running live
        return path_name

所以,那么你的应用程序代码看起来是这样的:

the_sound = pygame.mixer.Sound(my_path("the.wav"))
© www.soinside.com 2019 - 2024. All rights reserved.