tkinter TclError: error reading bitmap file
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)
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.
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()
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.