在Ubuntu上,无法在tkinter上插入图标



无法在ubuntu tkinter代码中添加icons

from tkinter import *

root = Tk()
root.title("Images and Icons")

icon = PhotoImage(file='tv_icon.ico')
root.tk.call('wm', 'iconphoto', root._w, icon)
root.mainloop()

输出为

Traceback (most recent call last):
File "icons.py", line 8, in <module>
icon = PhotoImage(file='tv_icon.ico')
File "/usr/lib/python3.8/tkinter/__init__.py", line 4061, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/usr/lib/python3.8/tkinter/__init__.py", line 4006, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "tv_icon.ico"

我在Ubuntu 20.04.3上遇到了同样的问题。

我不知道为什么会发生这种情况,但对我来说,这是有效的:

  1. .ico文件转换为.png文件
  2. root.iconphoto(False, PhotoImage(file='./icon.png'))

最新更新