我正在嘗試創建我的腳本的可執行檔案,但運行 .exe 找不到影像。我已經嘗試過 onefile 和 multiples 并將影像粘貼到里面,但它不起作用。
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)
root = Tk()
root.title("Tai Project")
root.geometry("600x600")
root.resizable(0, 0)
img = PhotoImage(file=resource_path("Tai_Project\ccc.png"))
img_opo = tkinter.PhotoImage(file=resource_path("Tai_Project\opo.png"))
img_label = tkinter.PhotoImage(file=resource_path("Tai_Project\labeltest.png"))
.spec
a = Analysis(
['Tai_Interface.py'],
pathex=['C:\\Users\\Usuario\\Desktop\\Python1\\Tai_Project'],
binaries=[],
datas=[('ccc.png','.'),('opo.png','.'),('labeltest.png','.')],
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=False,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
和錯誤資訊:
_tkinter.TclError:無法打開“C:\Users\Usuario\AppData\Local\Temp_MEI88482\Tai_Project\ccc.png”:沒有這樣的檔案或目錄
我的路線是:C:\Users\Usuario\Desktop\Python1\Tai_Project
uj5u.com熱心網友回復:
_tkinter.TclError:無法打開“C:\Users\Usuario\AppData\Local\Temp_MEI88482\Tai_Project\ccc.png”:沒有這樣的檔案或目錄
這意味著您的規范檔案寫入錯誤,并且您的檔案未包含在您的臨時目錄中。
pathex是您的 python.exe 檔案夾的路徑(不是專案)
資料必須將相對 TEMP 目錄路徑和絕對路徑連接到您的檔案
例如:
我有絕對路徑的檔案:
C:\Users\user\PycharmProjects\project\file.json
在代碼中,我這樣參考它:
resource_path("file.json")
規格檔案必須有:
datas = [("file.json","C:\\Users\\user\\PycharmProjects\\project\\file.json", "DATA")]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/477348.html
