Error Setting Application Icon in Tkinter (Python)

I'm trying to set an icon for a Tkinter window, but I receive the following error: _tkinter.TclError: error reading bitmap file "/home/user/.icons/app_icon.ico". The file is definitely located at the specified path.

Here is my code:

 
from tkinter import Tk

root = Tk() root.title("Application") root.iconbitmap('/home/user/.icons/app_icon.ico') # Set the icon root.mainloop()

What is causing this error?

Answers

  • Gravatar — онлайн-сервис, позволяющий пользователям Интернета поддерживать постоянную картинку на большинстве сайтов.

    Simon Schneider

    Yes, I'm using Linux.
    Thank you, I didn't know that .ico is not supported by Tkinter on Linux.

    0
  • Gravatar — онлайн-сервис, позволяющий пользователям Интернета поддерживать постоянную картинку на большинстве сайтов.

    Lukas Bergström

    You can also set a colored icon using PhotoImage. This method supports .png and .gif formats.

     
    from tkinter import Tk, PhotoImage

    root = Tk() icon = PhotoImage(file='/home/user/.icons/app_icon.png') root.iconphoto(True, icon) root.mainloop()

    0
  • Gravatar — онлайн-сервис, позволяющий пользователям Интернета поддерживать постоянную картинку на большинстве сайтов.

    André Carvalho

    Are you running the code on a Linux system?

    If so, that might be the issue because the .ico format is often not supported in Linux for Tkinter, which may only work with .xbm or .xpm files. Try replacing the icon with a .xbm format by adding @ before the path.

    Then, the icon setting line would look like this:

     
    root.iconbitmap('@/home/user/.icons/app_icon.xbm')

    0