将使用多个图像资源的TKINTER脚本转换为.exe |图像没有这样的文件或目录
我正在尝试创建脚本的可执行文件,但是运行.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"))
。
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,
)
C:\ users \ usuario \ desktop \ python1 \ tai_project
I am trying to create an executable of my script, but running the .exe does not find the image. I have tried both onefile and multiples and pasting the images inside but it does not work.
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,
)
And the error message:
_tkinter.TclError: couldn't open "C:\Users\Usuario\AppData\Local\Temp_MEI88482\Tai_Project\ccc.png": no such file or directory
And my route is:
C:\Users\Usuario\Desktop\Python1\Tai_Project
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这意味着您的规格文件写入错误,并且您的文件未包含在您的临时dir中。
pathEx 是通往您的python.exe文件夹的路径(不是项目)
datas 必须将相对临时路径和绝对路径连接到您的文件
:
我的文件具有绝对路径:
在代码中,我是这样的:
规格文件必须具有:
This means that your spec file is written wrong and your files are not included in your temp dir.
pathex is path to your python.exe folder (not project)
datas must connect relative TEMP dir paths and absolute path to your files
For example:
I have file with absolute path:
In code I refer to it like this:
Spec file must have: