如何将使用多个图像资源的tkinter脚本转换为.exe | image 没有那个文件或目录
How to convert tkinter script which uses multiple image resources to .exe | image no such file or directory
我正在尝试创建脚本的可执行文件,但是 运行.exe 找不到图像。我已经尝试了一个文件和多个文件并将图像粘贴到里面但是它不起作用。
这些是我的图片。
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")
您可以对所有路径使用此功能:
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 目录中使用。
实际上.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
我正在尝试创建脚本的可执行文件,但是 运行.exe 找不到图像。我已经尝试了一个文件和多个文件并将图像粘贴到里面但是它不起作用。
这些是我的图片。
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")
您可以对所有路径使用此功能:
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 目录中使用。
实际上.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