Discussion:
[Tkinter-discuss] Application window icon on Linux
Mark Summerfield
2012-06-07 16:18:25 UTC
Permalink
Hi,

I am struggling to get an application icon to show up on Linux.
(Although it works perfectly on Windows.)

I know this issue has come up before and I've followed the advice: I
made the icon 16x16, XBM format, and prefixed the full path with '@'.

I am using Python 3.2.2 and Tcl/Tk 8.5.

Here is a complete example:

#!/usr/bin/env python3
import os
from tkinter import *
from tkinter.ttk import *

class Window(Frame):
def __init__(self, parent):
super().__init__(parent)
quitButton = Button(self, text="Quit",
command=self.master.destroy)
quitButton.pack()
self.pack()
self.set_icon(os.path.join(os.getcwd(), "hello"))
# hello.xbm, hello.ico; both 16x16

def set_icon(self, iconName):
windowSystem = self.master.tk.call("tk", "windowingsystem")
if windowSystem == "win32": # Windows
iconName += ".ico"
elif windowSystem == "x11": # Unix
iconName = "@" + iconName + ".xbm"
if windowSystem != "aqua" and os.path.isfile(iconName):
print(iconName)
self.master.iconbitmap(iconName)
#self.master.wm_iconbitmap(iconName) also works

app = Tk()
app.title("Hello")
Window(app)
app.mainloop()

This works perfectly on Windows 7. But on Linux (Debian & Ubuntu), I
just get a default icon. The print() statement produces:
@/home/mark/hello.xbm

This hello.xbm file's contents are:

#define hello_width 16
#define hello_height 16
static unsigned char hello_bits[] = {
0x00, 0x00, 0x0e, 0x70, 0x0e, 0x70, 0x0e, 0x70, 0x0e, 0x70, 0x0e, 0x70,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0x0e, 0x70, 0x0e, 0x70, 0x0e, 0x70,
0x0e, 0x70, 0x0e, 0x70, 0x0e, 0x70, 0x00, 0x00 };

Thanks!
--
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
"Programming in Python 3" - ISBN 0321680561
http://www.qtrac.eu/py3book.html
Michael Lange
2012-06-07 17:01:32 UTC
Permalink
Hi Mark,

Thus spoketh Mark Summerfield <list at qtrac.plus.com>
Post by Mark Summerfield
This works perfectly on Windows 7. But on Linux (Debian & Ubuntu), I
@/home/mark/hello.xbm
I had to look twice myself, even if you showed the solution yourself in
the sentence above: of course os.path.isfile('@/home/mark/hello.xbm')
will always be False ;) If you change your set_icon() function to use a
simple try...except instead of additional tests as in

def set_icon(self, iconName):
windowSystem = self.master.tk.call("tk", "windowingsystem")
if windowSystem == "win32": # Windows
iconName += ".ico"
elif windowSystem == "x11": # Unix
iconName = "@" + iconName + ".xbm"
try:
self.master.iconbitmap(iconName)
except TclError:
pass

it should save you some headaches and (hopefully) work fine as long as
the bitmap file is present.

Regards

Michael

.-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-.

Without followers, evil cannot spread.
-- Spock, "And The Children Shall Lead", stardate 5029.5
Mark Summerfield
2012-06-08 07:00:12 UTC
Permalink
Hi Michael,

OK that was silly of me! It works perfectly now -- thanks:-)

BTW I haven't seen any nice pure Python ways to set the Windows taskbar
icon (although it "just works" on Linux).
--
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
"Programming in Go" - ISBN 0321774639
http://www.qtrac.eu/gobook.html
Loading...