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?

nbro
  • 15,395
  • 32
  • 113
  • 196
Jacob Vlijm
  • 3,099
  • 1
  • 21
  • 37

8 Answers8

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)
nbro
  • 15,395
  • 32
  • 113
  • 196
Jacob Vlijm
  • 3,099
  • 1
  • 21
  • 37
  • 3
    Work also for Python2.7 ! I was looking for this since several days. Thx – ericc Mar 19 '13 at 18:30
  • im trying to do this but im getting this 'Traceback (most recent call last): File "C:\Python27\Calc.pyw", line 58, in 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"' – Serial Apr 22 '13 at 05:45
  • 3
    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`. – esmit Dec 23 '13 at 18:39
  • 2
    works on windows with a `.png` too. use PIL pillow-2.5.1-win32-py27. – minghua Aug 07 '14 at 22:01
  • 3
    You should be using `root.iconphoto(True, img)`, rather than the call to tk. – Morgoth Aug 22 '17 at 15:16
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()
Alexx Roche
  • 3,151
  • 1
  • 33
  • 39
Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
  • 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? – Hubro Oct 19 '15 at 10:52
  • 1
    You'll have to give me a shitload more information than that. Like point me to your code and your icon file. – Lee Daniel Crocker Mar 24 '16 at 05:25
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.

nbro
  • 15,395
  • 32
  • 113
  • 196
Micrified
  • 3,338
  • 4
  • 33
  • 59
7

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.

Brōtsyorfuzthrāx
  • 4,387
  • 4
  • 34
  • 56
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)
ashwin ram kumar
  • 159
  • 2
  • 13
0

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

ex:

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

Elsayeda
  • 19
  • 3
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.

vpap
  • 1,389
  • 2
  • 21
  • 32
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.

DarenW
  • 16,549
  • 7
  • 63
  • 102