我正在尝试为我的脚本创建一个可执行文件,但是运行.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_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,
)以及错误消息:
"C:\Users\Usuario\AppData\Local\Temp_MEI88482\Tai_Project\ccc.png"::无法打开_tkinter.TclError,没有这样的文件或目录
我的路线是: C:\Users\Usuario\Desktop\Python1\Tai_Project
发布于 2022-05-12 13:01:46
"C:\Users\Usuario\AppData\Local\Temp_MEI88482\Tai_Project\ccc.png":
_tkinter.TclError:无法打开任何这样的文件或目录
这意味着您的spec文件写错了,您的文件没有包含在临时目录中。
pathex是指向python.exe文件夹的路径(而不是项目)
数据必须将相对临时dir路径和绝对路径连接到您的文件
例如:
我有带有绝对路径的文件:
C:\Users\user\PycharmProjects\project\file.json在代码中,我这样引用它:
resource_path("file.json")Spec文件必须具有:
datas = [("file.json","C:\\Users\\user\\PycharmProjects\\project\\file.json", "DATA")]https://stackoverflow.com/questions/72215773
复制相似问题