16

Well I have this:

import tkinter
gui = tkinter.Tk()
gui.iconbitmap(default='/home/me/PycharmProjects/program/icon.ico')
gui.mainloop()`

But when I run I get an error saying

Traceback (most recent call last):
File "/home/spencer/PycharmProjects/xMinecraft/GUI.py", line 17, in <module>
gui.iconbitmap(default='/home/me/PycharmProjects/program/icon.ico')
File "/usr/lib/python3.3/tkinter/__init__.py", line 1638, in wm_iconbitmap
return self.tk.call('wm', 'iconbitmap', self._w, '-default', default)
_tkinter.TclError: wrong # args: should be "wm iconbitmap window ?bitmap?"`

I'm trying to use tkinter to set a window I've made's icon. I'm using Pycharm installed on ubuntu 13.10. I've tried various things from changing '/' to '\' and adding a Z:// to the front because that's my partition's name. But I still get the error so please help.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Phoenix
  • 365
  • 3
  • 5
  • 12
  • Why use tkinter instead of the latest GTK3? http://python-gtk-3-tutorial.readthedocs.org/en/latest/ – NoBugs Dec 31 '13 at 16:35
  • It's already confirmed that tkinter doesn't work well anymore in Python 3.*. –  Dec 31 '13 at 16:36
  • 2
    @Allendar: what do you mean that it's been confirmed that it doesn't work well in python 3? It works fine. – Bryan Oakley Dec 31 '13 at 16:38
  • Huh okay. It worked in windows. – Phoenix Dec 31 '13 at 16:38
  • Yes it works, but many dependencies don't work well anymore, because they are either deprecated or not update (yet) for Python 3. Like NoBugs says; you might choose to start using GTK3. tkinter will only give more and more issues if you continue developing in it in Python 3. –  Dec 31 '13 at 16:40
  • 1
    I'll give GTK3 a shot, thanks guys. – Phoenix Dec 31 '13 at 16:41
  • @Allendar: that's just nonsense. Tkinter doesn't have any deprecated or missing dependencies. It is a perfect choice for someone learning to do GUI programming. GTK3 is fine too, but why introduce a dependency on an external package when you don't have to? – Bryan Oakley Dec 31 '13 at 16:46
  • 1
    You obviously haven't used tkinter for a longer period in Python 3. It wasn't that great in Python 2 for all-round GUI development. In Python 3 it's absolute garbage. I'm not being pessimistic here. It's just a painful reality. –  Dec 31 '13 at 16:49
  • @NoBugs: one reason is that GTK+ uses the LGPL license, which many people find too restrictive. Also, it adds an external dependency which can complicate installation. – Bryan Oakley Dec 31 '13 at 16:51
  • @Allendar: I don't understand your comment "wasn't that great in Python 2 for all-round GUI development". It's *fantastic* for "all-around GUI development". I have no idea where you're coming from. It's not great for polished, commercial apps, but for general all-around GUI development, there's really nothing better (several equals, but all toolkits have strengths and weaknesses). – Bryan Oakley Dec 31 '13 at 16:52
  • 2
    @Allendar: you say it's been confirmed that Tkinter doesn't work well in python 3. Can you provide links to that confirmation? I'm sure people who are deciding between toolkits would benefit from what you've found. – Bryan Oakley Dec 31 '13 at 16:54
  • Phoenix says it's for Ubuntu, which is using GTK for all its native controls and apps (Nautilus/unity/etc), so it's not some random extra dependency, it's the natural choice. In Windows, yes it would require looking up and downloading the GTK and GTK Python bindings. – NoBugs Dec 31 '13 at 17:01

9 Answers9

19

You need to either specify the path as the first positional argument, or use the keyword argument "bitmap". It's rather poorly documented, but the bitmap argument is required; you can't just give the default keyword argument. In fact, the bitmap keyword argument has been removed in python 3.

However, you can only use .ico files on windows. On ubuntu and other linux boxes you need to use a .xbm file, and need to prefix it with "@"

This should work on windows only:

gui.iconbitmap('/home/me/PycharmProjects/program/icon.ico')

On ubuntu, it would need to be something like this:

gui.iconbitmap('@/home/me/PyCharmProjets/program/icon.xbm')

You can't just rename a .ico file to .xbm, they are completely different file formats.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • @Phoenix: I don't think what you say is possible. You might get a different error, but you won't get the same error. – Bryan Oakley Dec 31 '13 at 16:59
  • @Phoenix: yep, that's a different error. I failed to see before replying that you're trying this on ubuntu rather than windows (I assumed windows because of the .ico extension). You need to give it an .xbm file, not a .ico file. – Bryan Oakley Dec 31 '13 at 17:09
  • Hmm I converted to .xbm and its all black with a little bit of white squiggles. – Phoenix Dec 31 '13 at 17:20
  • @Phoenix: xbm is a single color format -- pixels are either black or white. Converting a color image to xbm will almost always result in poor quality images. – Bryan Oakley Dec 31 '13 at 17:26
  • So I can't have colored icons? – Phoenix Dec 31 '13 at 17:54
8

Interesting bit of research

png, svg, ico didn't work

I found one xbm on my machine (xubuntu - Linux dist) , thanks to sqlitemanager

tool.xbm

note the @ - the code is a modification of Lutz "Programming Python" Chapter 1, tkinter103.py

from tkinter import *
from tkinter.messagebox import showinfo

def reply(name):
    showinfo(title='Reply', message='Hello %s!' % name)

top = Tk()
#img = PhotoImage(file='py-blue-trans-out.ico') #no

top.title('Echo')
top.iconbitmap('@tool.xbm') #yes
#top.iconphoto(True, PhotoImage(file='tool.xbm')) #no

Label(top, text="Enter your name:").pack(side=TOP)
ent = Entry(top)
ent.pack(side=TOP)
btn = Button(top, text="Submit", command=(lambda: reply(ent.get())))
btn.pack(side=LEFT)

top.mainloop()
Kev Youren
  • 91
  • 1
  • 1
4

Still in 2018 a high Rank google question. what works for me in python3 is to use ico in Windows and gif in Linux :

if ( sys.platform.startswith('win')): 
    gui.iconbitmap('logo_Wicon.ico')
else:
    logo = PhotoImage(file='logo.gif')
    gui.call('wm', 'iconphoto', gui._w, logo)
  • Trying a part of your code in Ubuntu 20.04 (python3.6) Right icon in the program list (in the left side of screen), Stretched icon with only black and white colours in the top panel (left to language - settings - ...). Any update for this? – Chris P Jun 16 '20 at 20:57
3

There are two ways,

1) use xbm file in ubuntu as ubuntu will not able to read ico files. but issue here is xbm can display only black and white images.

2) use tkinter.photoimage to display icon image like below,

 img = PhotoImage(file='your-icon')

 root.tk.call('wm', 'iconphoto', root._w, img)

issue here is photoimage can read only GIF and PGM/PPM images.

see details here - https://stackoverflow.com/a/11180300

Community
  • 1
  • 1
Som
  • 1,467
  • 13
  • 11
2

To display colored icons in linux you need to do it as shown below:

import tkinter
window = tkinter.Tk()
window.title("My Application")
img = tkinter.PhotoImage(file='~/pharmapos/pharmapos.png')
window.tk.call('wm', 'iconphoto', window._w, img)
window.mainloop()
Chirag
  • 1,189
  • 2
  • 21
  • 43
0

I had to convert to an XBM format and use the following root.iconbitmap('@imagename.xbm') however my platform is Ubuntu and I discovered my os theme has no spot for he image....

Raymond
  • 40
  • 8
0

this worked for me in linux mint:

from tkinter import *
from PIL import Image, ImageTk
main_fn=Tk()
log= Image.open("path_to_image.ico")
logo = ImageTk.PhotoImage(log)
main_fn.tk.call('wm', 'iconphoto', main_fn._w, logo)
main_fn.mainloop()
  • [A code-only answer is not high quality](https://meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers). While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please [edit] your answer to include explanation and link to relevant documentation. – Muhammad Mohsin Khan Mar 16 '22 at 16:21
0

We can use iconphoto on linux. Colored icon works well too. You can use .png files. The .ico file can be converted using 'convert' utility.

convert icon.ico icon.png

First create a PhotoImage widget:

icon = tkinter.PhotoImage(file='icon.png')

Then use iconphoto to change the icon:

root = Tk()
root.iconphoto(False, icon)

Reference: Please have a look at this link

-2

import tkinter gui = tkinter.Tk() gui.iconbitmap() gui.mainloop()

In place of gui.iconbitmap(default='/home/me/PycharmProjects/program/icon.ico') i used gui.iconbitmap() this just works for me.