tkinter TclError: error reading bitmap file
Asked Answered
R

9

26

I am trying to set an application icon (python3 / tkinter) like this:

Interface()
root.title("Quicklist Editor")
root.iconbitmap('@/home/jacob/.icons/qle_icon.ico')
root.resizable(0, 0)
root.mainloop()

no matter what I do, I keep getting an error message (Idle), saying:

return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
_tkinter.TclError: error reading bitmap file "/home/jacob/.icons/qle_icon.ico"

What am I doing wrong?

Recur answered 24/6, 2012 at 10:18 Comment(4)
I believe an .ico file will not work for this(at least on linux). Try using a .xbm or .xpm file instead.Extramundane
...thanks! unfortunately didn't work either, also tried .pngRecur
@Jacob Stupid question: Is your icon actually at /home/jacob/.icons/qle_icon.ico? ;)Gussy
@Gussy definitely not a stupid question, and I checked a few times, as one starts doubting oneselves when things do not work...Recur
R
71

The problem is not the code, but the icon. I tried creating an xbm with another program than Gimp (some KDE icon editor), and although it looks terrifyingly ugly, it does show an icon. I guess I have to find a creator that gives an "understandable" icon for my Python program.


Edit

The iconbitmap method turned out to be black and white only, so it was useless after all.

After a long search, I found the solution to set the color of an application's icon for Python 3 (on Linux). I found it here:

root = Tk()
img = PhotoImage(file='your-icon')
root.tk.call('wm', 'iconphoto', root._w, img)
Recur answered 24/6, 2012 at 18:54 Comment(5)
Work also for Python2.7 ! I was looking for this since several days. ThxAx
im trying to do this but im getting this 'Traceback (most recent call last): File "C:\Python27\Calc.pyw", line 58, in <module> img = PhotoImage(file='icon!.ico') File "C:\Python27\lib\lib-tk\Tkinter.py", line 3244, in init Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Python27\lib\lib-tk\Tkinter.py", line 3200, in init self.tk.call(('image', 'create', imgtype, name,) + options) TclError: couldn't recognize data in image file "icon!.ico"'Gideon
The documentation of Tkinter.PhotoImage says that it only accepts gif or PPM/PGM formatted images. On a linux machine, it worked for me with a .gif, but not a .png.Spandrel
works on windows with a .png too. use PIL pillow-2.5.1-win32-py27.Wu
You should be using root.iconphoto(True, img), rather than the call to tk.Gluttony
M
20

This is an old question, and there is lots of stuff written about it on the web, but all of it is either incorrect or incomplete, so having gotten it to work I thought it would be good to record my actual working code here.

First, you'll need to create an icon and save it in two formats: Windows "ico" and Unix "xbm". 64 x 64 is a good size. XBM is a 1-bit format--pixels just on or off, so no colors, no grays. Linux implementations of tkinter only accept XBM even though every Linux desktop supports real icons, so you're just out of luck there. Also, the XBM spec is ambiguous about whether "on" bits represent black or white, so you may have to invert the XBM for some desktops. Gimp is good for creating these.

Then to put the icon in your titlebar, use this code (Python 3):

import os
from tkinter import *
from tkinter.ttk import *

root = Tk()
root.title("My Application")
if "nt" == os.name:
    root.wm_iconbitmap(bitmap = "myicon.ico")
else:
    root.wm_iconbitmap(bitmap = "@myicon.xbm")

root.mainloop()
Mccray answered 23/4, 2013 at 5:23 Comment(2)
What the hell, is this real life? Tk on Linux only supports garbage icons? And as a bonus it doesn't support native Gtk or qt widgets. How is anybody supposed to take it seriously as a GUI library?Philippines
You'll have to give me a shitload more information than that. Like point me to your code and your icon file.Mccray
D
8

This will allow you to use PNG files as icons, and it does render color. I tested it on Xubuntu 14.04, 32-bit with Python 3.4 (root is your Tk object):

import sys, os
program_directory=sys.path[0]
root.iconphoto(True, PhotoImage(file=os.path.join(program_directory, "test.png")))

(Finding program directory is important if you want it to search for test.png in the same location in all contexts. os.path.join is a cross-platform way to add test.png onto the program directory.)

If you change True to False then it won't use the same icon for windows that aren't the main one.

Please let me know if this works on Windows and Mac.

Divertissement answered 9/8, 2014 at 21:42 Comment(0)
P
7

I tried this, and I couldn't get it to work using Windows 7.

Found a fix.

Use Jacob's answer, but the file has to be a .gif if you're using my OS, (Windows 7) it appears.

Make a 64x64 gif using MS paint, save it, use the file path and bingo, works.

Protohuman answered 22/6, 2013 at 14:13 Comment(0)
H
3

I hope this helps you for cross-platform ability

LOGO_PATH="pic/logo.ico"
LOGO_LINUX_PATH="@pic/logo_1.xbm"  #do not forget "@" symbol and .xbm format for Ubuntu 
root = Tk()
    if detect_screen_size().detect_os()=="Linux":
        root.iconbitmap(LOGO_LINUX_PATH)
    else:
        root.iconbitmap(LOGO_PATH)
Heifetz answered 14/1, 2020 at 14:47 Comment(0)
L
0

Simply using an r string to convert the directory into raw text worked for me:

ex:

app.iconbitmap(r'enter your path here')

Lowborn answered 19/5, 2021 at 13:57 Comment(0)
P
0

In my case, Ubuntu 20.04, python 3.6 (conda), the command iconbitmap(bitmap=icon_path) failed w/ this error. In the end, I put the command w/in a try-except block and it worked; I can see the colorful image.

Promenade answered 6/7, 2022 at 14:10 Comment(0)
D
0

I'm surprised to see this is such an old question with no good answers, not in eight years! I too want my own icon for my "quickie" tkinter program.

What does work for me on Linux and Python3:

#!/usr/bin/env python
import tkinter
from PIL import Image, ImageTk

root = tkinter.Tk()
im = Image.open('junk.png')
photo = ImageTk.PhotoImage(im)
root.wm_iconphoto(True, photo)
root.mainloop()

The key seems to be using Image and ImageTk. I found zero solutions that worked without these.

Doordie answered 25/12, 2022 at 4:13 Comment(0)
K
0

In other excellent answers I hear complaints about the lousy non-color icons in xbm for linux. But the original question does not specify the operating system.

For Linux the answer seems today, as of december 2023, that you don't need an icon file in your app anymore, whether color or xbm. The reason is that, on windows, while some apps still show an icon in the app title bar, such as Gimp, KeePass, DBeaver, a lot of apps do not, like: command terminal, Firefox, File Explorer, Google Chrome.

On Linux, such as Ubuntu and Raspberry Pi, these apps, in fact all apps that I checked today, do NOT show the icon in the application title bar. These apps do show the app icon in the system task bar, as specified in the app.desktop file, hence the app does not need to be programmed in the app itself, as described in the other answers here.

It seems that the icon in the app title bar is getting out of fashion, and that Windows lags behind Linux in this respect. The reason seems that in this way application real estate (screen space) is used more efficiently. Modern apps show instead the icons of the file or site that is open in the respective tabs of the app.

This all might be the reason that I could not get the other answers to work for my linux python app.

TLDR: for Linux, this question does not need any application code, same for modern Windows apps.

Kimberlykimberlyn answered 11/12, 2023 at 19:5 Comment(2)
While this may be off topic, many of the "solutions" are advising to use root.iconphoto and while this doesn't seem to work to add the icon image in the app title bar (as you correctly mention), it will add the icon in the panel while the app is running so I would say that there is value in configuring an icon.Loriloria
@Loriloria Sure, just don't waste a lot of time with trying to get the icon in the app title bar. If that does not work, it is a feature, not a bug :-)Kimberlykimberlyn

© 2022 - 2025 — McMap. All rights reserved.