我正在尝试创建我的脚本的可执行文件,但运行.exe没有找到图像。我已经尝试了一个文件和多个并粘贴图像,但它不起作用。
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_Projectccc.png"))
img_opo = tkinter.PhotoImage(file=resource_path("Tai_Projectopo.png"))
img_label = tkinter.PhotoImage(file=resource_path("Tai_Projectlabeltest.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: cannot open "C:UsersUsuarioAppDataLocalTemp_MEI88482Tai_Projectccc.png": no such file or directory
我的路线是:C:UsersUsuarioDesktopPython1 Tai_Project
_tkinter。TclError:无法打开"C:UsersUsuarioAppDataLocalTemp_MEI88482Tai_Projectccc.png": no该文件或目录
这意味着你的spec文件写错了,你的文件没有包含在你的临时目录中。
pathexpython.exe文件夹(不是项目)的路径
数据必须将相对TEMP目录路径和绝对路径连接到文件
例如:
我有一个绝对路径文件:
C:UsersuserPycharmProjectsprojectfile.json
在代码中我这样引用它:
resource_path("file.json")
规格文件必须有:
datas = [("file.json","C:\Users\user\PycharmProjects\project\file.json", "DATA")]