Python 3 tkinter iconbitmap error in ubuntu

2019-02-12 23:18发布

问题:

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.

回答1:

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.



回答2:

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()


回答3:

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)


回答4:

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



回答5:

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....



回答6:

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.