将使用多个图像资源的TKINTER脚本转换为.exe |图像没有这样的文件或目录

发布于 2025-01-28 16:57:37 字数 1575 浏览 5 评论 0原文

我正在尝试创建脚本的可执行文件,但是运行.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人 2025-02-04 16:57:37

_tkinter.tclerror:无法打开“ c:\ users \ usuario \ appdata \ local \ local \ temp_mei88482 \ tai_project \ ccc.png”:no
这样的文件或目录

这意味着您的规格文件写入错误,并且您的文件未包含在您的临时dir中。

pathEx 是通往您的python.exe文件夹的路径(不是项目)

datas 必须将相对临时路径和绝对路径连接到您的文件

我的文件具有绝对路径:

C:\Users\user\PycharmProjects\project\file.json

在代码中,我是这样的:

resource_path("file.json")

规格文件必须具有:

datas = [("file.json","C:\\Users\\user\\PycharmProjects\\project\\file.json", "DATA")]

_tkinter.TclError: couldn't open "C:\Users\Usuario\AppData\Local\Temp_MEI88482\Tai_Project\ccc.png": no
such file or directory

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:

C:\Users\user\PycharmProjects\project\file.json

In code I refer to it like this:

resource_path("file.json")

Spec file must have:

datas = [("file.json","C:\\Users\\user\\PycharmProjects\\project\\file.json", "DATA")]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文