Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Application icon in my python Tk base application (On Ubuntu)

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?

like image 834
hardythe1 Avatar asked Dec 01 '22 19:12

hardythe1


2 Answers

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.

like image 133
patthoyts Avatar answered Dec 04 '22 10:12

patthoyts


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()
like image 30
Fidel Jimenez Sanzano Avatar answered Dec 04 '22 08:12

Fidel Jimenez Sanzano