5

I want to set an image in my GUI application built on Python Tk package.

I tried this code:

root.iconbitmap('window.xbm')

but it gives me this:

root.iconbitmap('window.xbm')
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1567, in wm_iconbitmap
    return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
_tkinter.TclError: bitmap "window.xbm" not defined

Can anyone help?

kiri
  • 2,522
  • 4
  • 26
  • 44
hardythe1
  • 104
  • 1
  • 1
  • 6

3 Answers3

15

You want to use wm iconphoto. Being more used to Tcl/Tk than Python Tkinter I don't know how that is exposed to you (maybe root.iconphoto) but it takes a tkimage. In Tcl/Tk:

image create photo applicationIcon -file application_icon.png
wm iconphoto . -default applicationIcon

In Tk 8.6 you can provide PNG files. Before that you have to use the TkImg extension for PNG support or use a GIF. The Python PIL package can convert images into TkImage objects for you though so that should help.

EDIT

I tried this out in Python as well and the following worked for me:

import Tkinter
from Tkinter import Tk
root = Tk()
img = Tkinter.Image("photo", file="appicon.gif")
root.tk.call('wm','iconphoto',root._w,img)

Doing this interactively on Ubuntu resulted in the application icon (the image at the top left of the frame and shown in the taskbar) being changed to use my provided gif image.

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
patthoyts
  • 32,320
  • 3
  • 62
  • 93
4

This worked for me

from tkinter import *       

raiz=Tk()
raiz.title("Estes es el titulo")
img = Image("photo", file="pycharm.png")
raiz.tk.call('wm','iconphoto',raiz._w, img)
raiz.mainloop()
0

Try this:

root.iconbitmap('@window.xbm')

And quote:

Set (get) the icon bitmap to use when this window is iconified. This method are ignored by some window managers (including Windows).

Note that this method can only be used to display monochrome icons. To display a color icon, put it in a Label widget and display it using the iconwindow method instead.

Community
  • 1
  • 1
kalgasnik
  • 3,149
  • 21
  • 19