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

发布于 2025-01-28 08:52:10 字数 353 浏览 4 评论 0原文

我正在尝试创建脚本的可执行文件,但是运行.exe找不到图像。我已经尝试了OneFile和Multiples并将其粘贴在内部,但它不起作用。

这些将是我的图像。

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")

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.

These would be my images.

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")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

硪扪都還晓 2025-02-04 08:52:10

您可以在所有路径上使用此功能:

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目录中使用,运行程序时创建。

You can use this function for all paths:

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)

Usage example:

img = PhotoImage(file=resource_path("Tai_Project\ccc.png"))

Also in your .spec file you need to match directories in "datas" section like this:

a.datas += [("Tai_Project\ccc.png","C:\\Users\\username\\projects\\my_project\\Tai_Project\\ccc.png", "DATA")]

This way your files will be included in .exe and will be available in TEMP directory which is created when you run your program.

捂风挽笑 2025-02-04 08:52:10

实际上.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

Actually .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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文