我正在嘗試創建我的腳本的可執行檔案,但運行 .exe 找不到影像。我已經嘗試過 onefile 和 multiples 并將影像粘貼到里面,但它不起作用。
這些將是我的影像。
root = Tk()
root.title("Tai Project")
root.geometry("600x600")
root.resizable(0, 0)
img = PhotoImage(file="Tai_Project\ccc.png")
img_opo = PhotoImage(file="Tai_Project\opo.png")
img_label = PhotoImage(file="Tai_Project\labeltest.png")
uj5u.com熱心網友回復:
您可以將此函式用于所有路徑:
import sys
import os
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
使用示例:
img = PhotoImage(file=resource_path("Tai_Project\ccc.png"))
同樣在您的 .spec 檔案中,您需要匹配“資料”部分中的目錄,如下所示:
a.datas = [("Tai_Project\ccc.png","C:\\Users\\username\\projects\\my_project\\Tai_Project\\ccc.png", "DATA")]
這樣,您的檔案將包含在 .exe 中,并且可以在運行程式時創建的 TEMP 目錄中使用。
uj5u.com熱心網友回復:
實際上 .spec
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(
['Tai_Interface.py'],
pathex=[],
binaries=[],
datas=[("Tai_Project\\ccc.png","C:\\Users\\Usuario\\Desktop\\Python1\\Tai_Project\\ccc.png", "DATA"), ("Tai_Project\\ccc.png","C:\\Users\\Usuario\\Desktop\\Python1\\Tai_Project\\odo.png", "DATA"), ("Tai_Project\\ccc.png","C:\\Users\\Usuario\\Desktop\\Python1\\Tai_Project\\labeltest.png", "DATA")],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='Tai_Interface',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
Traceback (most recent call last):
File "Tai_Interface.py", line 28, in <module>
File "tkinter\__init__.py", line 4064, in __init__
File "tkinter\__init__.py", line 4009, in __init__
_tkinter.TclError: couldn't open "C:\Users\Usuario\Desktop\Python1\Tai_Project\dist\Tai_Interface.exe\Tai_Project\ccc.png": no such file or directory
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/477349.html
標籤:Python tkinter 包裹 可执行程序 软件分发
上一篇:將使用多個影像資源的tkinter腳本轉換為.exe|影像沒有這樣的檔案或目錄
下一篇:在JUnit5中,為什么postMethod會拋出TransactionSystemException而不是回傳帶有錯誤請求的ResponseEntity?
