1 回答

TA貢獻1827條經驗 獲得超9個贊
您將需要創建一個 .spec 文件并提及您要在其中包含的內容,例如字體或其他圖像。
# -*- mode: python -*-
block_cipher = None
a = Analysis(['your python file.py'],
pathex=['C:\\path\\to\\directory'], # just the directory not the file
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
a.datas += [('ttf file','path\\to\\ttf\\file', "DATA")]
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='Your App Name',
debug=False,
strip=False,
upx=True,
console=False # set True if command prompt window needed
)
在游戲所在的實際 python 文件中包含此內容。
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
每當您想加載字體時,都可以這樣做。
font = pygame.font.Font(resource_path("Font.ttf"), size)
在你的主 python 文件中有了它之后,在你的終端中輸入你創建的規范文件。
pyinstaller yourspecfile.spec
這將創建您的可執行文件,它應該自己工作。
這是我以前項目中的規范文件的樣子 # - - mode: python - -
block_cipher = None
a = Analysis(['lol.py'],
pathex=['C:\\games\\snake'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
a.datas += [('8min.ttf','C:\\games\\snake\\8min.ttf', "DATA")]
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='Snake',
debug=False,
strip=False,
upx=True,
console=False)
添加回答
舉報