24

我正在尝试像这样设置应用程序图标(python3 / tkinter):

Interface()
root.title("Quicklist Editor")
root.iconbitmap('@/home/jacob/.icons/qle_icon.ico')
root.resizable(0, 0)
root.mainloop()

无论我做什么,我都会收到一条错误消息(空闲),说:

return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
_tkinter.TclError: error reading bitmap file "/home/jacob/.icons/qle_icon.ico"

我究竟做错了什么?

4

6 回答 6

70

问题不在于代码,而在于图标。我尝试使用 Gimp(一些 KDE 图标编辑器)以外的其他程序创建一个xbm,虽然它看起来非常丑陋,但它确实显示了一个图标。我想我必须找到一个为我的 Python 程序提供“可理解”图标的创建者。


编辑

iconbitmap方法原来是黑白分明的,毕竟没用。

经过长时间的搜索,我找到了为 Python 3(在 Linux 上)设置应用程序图标颜色的解决方案。我在这里找到它:

root = Tk()
img = PhotoImage(file='your-icon')
root.tk.call('wm', 'iconphoto', root._w, img)
于 2012-06-24T18:54:11.893 回答
17

这是一个老问题,网上有很多关于它的东西,但所有这些都是不正确或不完整的,所以让它工作后,我认为在这里记录我的实际工作代码会很好。

首先,您需要创建一个图标并将其保存为两种格式:Windows“ico”和 Unix“xbm”。64 x 64 是一个不错的尺寸。XBM 是一种 1 位格式——像素只是打开或关闭,所以没有颜色,没有灰色。tkinter 的 Linux 实现只接受 XBM,即使每个 Linux 桌面都支持真正的图标,所以你在那里不走运。此外,XBM 规范对于“on”位是代表黑色还是白色并不明确,因此您可能必须为某些桌面反转 XBM。Gimp 非常适合创建这些。

然后将图标放在标题栏中,使用以下代码(Python 3):

import os
from tkinter import *
from tkinter.ttk import *

root = Tk()
root.title("My Application")
if "nt" == os.name:
    root.wm_iconbitmap(bitmap = "myicon.ico")
else:
    root.wm_iconbitmap(bitmap = "@myicon.xbm")

root.mainloop()
于 2013-04-23T05:23:46.903 回答
7

This will allow you to use PNG files as icons, and it does render color. I tested it on Xubuntu 14.04, 32-bit with Python 3.4 (root is your Tk object):

import sys, os
program_directory=sys.path[0]
root.iconphoto(True, PhotoImage(file=os.path.join(program_directory, "test.png")))

(Finding program directory is important if you want it to search for test.png in the same location in all contexts. os.path.join is a cross-platform way to add test.png onto the program directory.)

If you change True to False then it won't use the same icon for windows that aren't the main one.

Please let me know if this works on Windows and Mac.

于 2014-08-09T21:42:28.197 回答
7

我试过这个,但我无法使用 Windows 7 让它工作。

找到了解决办法。

.gif使用 Jacob 的答案,但如果您使用的是我的操作系统(Windows 7),该文件必须是一个。

使用 MS Paint 制作一个 64x64 的 gif,保存它,使用文件路径和宾果游戏,就可以了。

于 2013-06-22T14:13:53.907 回答
2

我希望这对您的跨平台能力有所帮助

LOGO_PATH="pic/logo.ico"
LOGO_LINUX_PATH="@pic/logo_1.xbm"  #do not forget "@" symbol and .xbm format for Ubuntu 
root = Tk()
    if detect_screen_size().detect_os()=="Linux":
        root.iconbitmap(LOGO_LINUX_PATH)
    else:
        root.iconbitmap(LOGO_PATH)
于 2020-01-14T14:47:54.667 回答
0

只需使用 r 字符串将目录转换为原始文本对我有用:

前任:

app.iconbitmap(r'在这里输入你的路径')

于 2021-05-19T13:57:33.500 回答