CustomTkinter icon indicating copy to clipboard operation
CustomTkinter copied to clipboard

iconbitmap is not working when converted to .exe

Open shrivatsahosabettu opened this issue 2 years ago • 21 comments

In windows 11 with Python 3.11 iconbitmap is not working after converting the LinkApp.py to exe

  File "LinkApp.py", line 267, in <module>
  File "LinkApp.py", line 23, in __init__
  File "customtkinter\windows\ctk_tk.py", line 230, in iconbitmap
    super().wm_iconbitmap(bitmap, default)
  File "tkinter\__init__.py", line 2136, in wm_iconbitmap
_tkinter.TclError: bitmap "empty.ico" not defined

Name: customtkinter
Version: 5.1.3
Summary: Create modern looking GUIs with Python
Home-page: https://customtkinter.tomschimansky.com
Author: Tom Schimansky
Author-email:
License: Creative Commons Zero v1.0 Universal
Location: C:\Users\shosabet\AppData\Local\Programs\Python\Python311\Lib\site-packages
Requires: darkdetect
Required-by: CTkMessagebox, tkintermapview

Not sure if this issue mentioned is really fixed on .exe file.

image

Gone through packaging page and followed the steps not worked. my ico file and py file are both in the same folder

shrivatsahosabettu avatar Jun 07 '23 14:06 shrivatsahosabettu

@shrivatsahosabettu I guess, the executable is not able to find the bitmap icon file empty.ico. You have to pack it properly with pyinstaller.

Please read this guide for details: https://github.com/TomSchimansky/CustomTkinter/discussions/939

Akascape avatar Jun 07 '23 18:06 Akascape

Adding on to what @Akascape said, when building with Pyinstaller, you'll want to use the --add-data option to add the .ico file so that it can be accessed by your application. If you are talking about the application/exe icon itself, use the -i option.

The official Pyinstaller documentation might also help.

ghost avatar Jun 13 '23 23:06 ghost

Sure I will take a look, but so far I haven't got success, as I am looking for the title bar icon change. It works to change the title bar icon but when converted to exe not able to find out the location. I created icons folder and tried to place in the same folder where my python script reside, still not worked. Let me try the options specified. The guide which @Akascape mentioned itself is so long and not able to figure out what is the summary out of it. Need to study more and try. I will look into your options as well. Thanks much. Hope this resolves the issue.

shrivatsahosabettu avatar Jun 14 '23 04:06 shrivatsahosabettu

but when converted to exe not able to find out the location. I created icons folder and tried to place in the same folder where my python script reside, still not worked.

@shrivatsahosabettu - Based on your most recent comment, it looks like the problem is not with CustomTkinter (unable to find your icon file) but rather with the paths you've provided. I'd suggest checking your relative paths and trying to convert them to absolute paths.

ghost avatar Jun 14 '23 14:06 ghost

When developing my app with CustmTkinter, I found that the titlebar icon hardly ever worked on macOS (at least 12.6.6). I looked around issues and Discussions on CustomTkinter but found nothing (except the default icon I think). Make sure you and your users will be on Windows, it seems that Windows is less finicky (when it comes to titlebar icons).

ghost avatar Jun 14 '23 14:06 ghost

In windows 11 with Python 3.11 iconbitmap is not working after converting the LinkApp.py to exe

  File "LinkApp.py", line 267, in <module>
  File "LinkApp.py", line 23, in __init__
  File "customtkinter\windows\ctk_tk.py", line 230, in iconbitmap
    super().wm_iconbitmap(bitmap, default)
  File "tkinter\__init__.py", line 2136, in wm_iconbitmap
_tkinter.TclError: bitmap "empty.ico" not defined

Name: customtkinter
Version: 5.1.3
Summary: Create modern looking GUIs with Python
Home-page: https://customtkinter.tomschimansky.com
Author: Tom Schimansky
Author-email:
License: Creative Commons Zero v1.0 Universal
Location: C:\Users\shosabet\AppData\Local\Programs\Python\Python311\Lib\site-packages
Requires: darkdetect
Required-by: CTkMessagebox, tkintermapview

Not sure if this issue mentioned is really fixed on .exe file.

image

Gone through packaging page and followed the steps not worked. my ico file and py file are both in the same folder

I also got the same issue, works with vscode and not working when i convert to .exe. But instead of running auto-py-to-exe, just copy the code from the window of autopytoexe browser close it and paste in your terminal then pack will solve this issue. Atleast for mine :)

image

raja2505 avatar Jun 17 '23 07:06 raja2505

@raja2505 - Sorry, I'm a little confused.

Could you clarify on "code"? Do you mean just running the code with the python comand?

ghost avatar Jun 17 '23 16:06 ghost

@raja2505 I have python 3.11 I tried the code generated from the auto-py-to-exe run from the command window. It didn't worked for me. As of now I am dealing with default icon of the tkinter in the title bar.

shrivatsahosabettu avatar Jun 18 '23 08:06 shrivatsahosabettu

--onefile still gives me this error, image

but one directory with console is working image

raja2505 avatar Jun 19 '23 04:06 raja2505

@raja2505 - Sorry, I'm a little confused.

Could you clarify on "code"? Do you mean just running the code with the python comand?

image copy this and run with terminal --onefile gives me same error

raja2505 avatar Jun 19 '23 04:06 raja2505

I need to have onefile option only and it seems not working or probably need to provide some parameters while converting onefile exe. One Directory with console will work as the path will be found in the Directory structure. On onefile when lauched it will create directory with _MEIPASS, where the title bar icon is not able to find. I haven't figured it out yet the alternative or the parameter.

shrivatsahosabettu avatar Jun 19 '23 07:06 shrivatsahosabettu

@shrivatsahosabettu

On onefile when lauched it will create directory with _MEIPASS, where the title bar icon is not able to find.

You have to search the icon inside that _MEIPASS folder. That searching method is given here #939 (see the one-file part). Go through that guide properly.

Akascape avatar Jun 19 '23 08:06 Akascape

CustomTkinter's documentation for packaging specifies that the "--onefile" option doesn't work. You have to use the "--onedir" option. This option will create a "dist" folder with a directory and a bundled application ready for distribution. You can ignore the directory inside the "dist" folder.

ghost avatar Jun 19 '23 14:06 ghost

In the field:

windowname.iconbitmap()

Point the path of the .ico file as follows:

windowname.iconbitmap(r'filePath\file.ico') worked for me.

programadordeversos avatar Jun 30 '23 18:06 programadordeversos

@programadordeversos - Did it work when you packaged the app with pyinstaller?

ghost avatar Jun 30 '23 18:06 ghost

Yes, being --onefile.

I tried following, before pointing the file+file path with the --onedir but using --onedir I can't share just the executable, it depends on the components of the directory so I pointed it like windowname.iconbitmap(r'filePath\file. ico') and ran the pyinstaller command with --onefile and it worked.

Do the test.

programadordeversos avatar Jul 01 '23 01:07 programadordeversos

Got sometime to go through whole conversation #939 and thanks everyone, specially @Akascape and @dishb its working with onefile window option as well with Python 3.11 version(windows), just need to call the function(I provided below sample) and include the folder where your resources are kept in additional files section. Don't forget to add below command in settings "Manually Provide Options" --collect-all customtkinter --collect-all darkdetect --collect-all CTkMessagebox

Note: CTkMessagebox if you are using it

    def resource_path(self, 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)

I think we are good to close this one.

shrivatsahosabettu avatar Jul 01 '23 04:07 shrivatsahosabettu

@dishb

Follow the tip above from @shrivatsahosabettu and in the part of the script that deals with the Customtkinter window, insert the function:

import sys, os def resource(relative_path): base_path = getattr( sys, '_MEIPASS', os.path.dirname(os.path.abspath(icon folder and/or files))) return os.path.join(base_path, relative_path)

As much as the tip I gave above of pointing the file path\file name works, it only works on the computer that has this folder in its directories or mapped, that's why the need to point this function.

Conversation #939 explains better why to define this function.

Hope this helps.

programadordeversos avatar Jul 10 '23 17:07 programadordeversos

Ho avuto un po' di tempo per leggere tutta la conversazione #939 e ringrazio tutti, in particolare@AkascapeE@dishb funziona anche con l'opzione onefile window con la versione Python 3.11 (windows), devi solo chiamare la funzione (ho fornito l'esempio qui sotto) e includere la cartella in cui sono conservate le tue risorse nella sezione file aggiuntivi. Non dimenticare di aggiungere il comando qui sotto nelle _impostazioni _ "Manually Provide Options" --collect-all customtkinter --collect-all darkdetect --collect-all CTkMessagebox

Nota: CTkMessagebox se lo stai utilizzando

def resource_path(self, 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)

Penso che possiamo chiudere questa discussione.

Hi, I have the same problem as you, please could you insert the code in detail because I don't understand well and I don't speak English well

massimilianocom avatar Feb 18 '25 01:02 massimilianocom

For anyone wondering how to change the icon for the taskbar and top of the window after packing it into an exe. All you have to do is to name the favicon file 'CustomTkinter_icon_Windows.ico' add the icon file to your assets to overwrite the original icon and then use the iconbitmap method with an absolute path (at least on Windows) Here is how it looks in code

Run this in a separate file import PyInstaller.__main__ PyInstaller.__main__.run([ 'Program.py', '--noconfirm', '--onedir', '--windowed', '--icon=CustomTkinter_icon_Windows.ico', '--add-data=Absolute_Path_to_customtkinter;Program.py', '--add-data=CustomTkinter_icon_Windows.ico;Program.py/assets/icons', ])

I personally used this method for my ico file from PIL import Image img = Image.open("img.png") img.save("CustomTkinter_icon_Windows.ico", format="ICO", sizes=[(16, 16), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)])

Initialize your window this way window = ctk.CTk() window.iconbitmap("Absolute_path_to_CustomTkinter_icon_Windows.ico")

Let me know if this helps! Edited to delete personal info

Froconic avatar Jun 13 '25 21:06 Froconic

Next is to figure out how to get the ico file to be displayed in higher quality

Froconic avatar Jun 13 '25 21:06 Froconic