Home > database >  How to convert tkinter script which uses multiple image resources to .exe | image no such file or di
How to convert tkinter script which uses multiple image resources to .exe | image no such file or di

Time:05-13

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

CodePudding user response:

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.

CodePudding user response:

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
  • Related