Error running the executable file (Tkinter python)

141 views Asked by At

So I am using customtkinter for a small project and I made an executable file using pyinstaller. When I run the exe file I get the following error.

Traceback (most recent call last):
  File "main.py", line 3, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 391, in exec_module
  File "src\app\frontend\main_ui.py", line 250, in <module>
  File "src\app\frontend\main_ui.py", line 215, in __init__
  File "customtkinter\windows\ctk_tk.py", line 228, in wm_iconbitmap
  File "tkinter\__init__.py", line 2138, in wm_iconbitmap
_tkinter.TclError: bitmap "data\images\valeo.ico" not defined

I tried a few solutions from stackoverflow, but it doesn't seem to work, apparently this is an issue in Linux but I am using Windows 11.

I would appreciate it if someone helps me in this matter.

Thank you.

1

There are 1 answers

2
Dinux On

The error occurs because the .exe file either doesn't contain the file valeo.ico or the file can't be accessed. Make sure that the .exe contains the icon file. If it does, and you still get the error, in your .py file, you need to access the icon file in a different way. Example code:

#Import the necessary modules
from tkinter import *
import tkinter
import sys
import os

#Create a resource path function
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)

#Create the screen
root = Tk()
root.geometry("500x500")

#Set icon
root.iconbitmap(resource_path("data\images\valeo.ico"))

root.mainloop()

When Pyinstaller is run using the onefile option, a temporary folder structure is created. The path to this structure in stored in sys._MEIPASS. So, we append the folder structure path to the path of our image file.