【问题标题】:on Ubuntu, Not able to insert icon on tkinter在 Ubuntu 上,无法在 tkinter 上插入图标
【发布时间】:2021-10-04 01:44:04
【问题描述】:

无法在 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"

【问题讨论】:

  • .ico 不受 tkinter.PhotoImage() 支持,请改用 PNG 图片。
  • This 表示如果使用root.iconbitmap(...),则可以使用.ico 文件。而且大多数时候,使用.tk.call(...) 也不是一个好主意。通常tkinter定义了一个可以调用的普通函数。

标签: python tkinter


【解决方案1】:

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

我不知道为什么会这样,但对我来说这很有效:

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

【讨论】:

    猜你喜欢
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 2010-11-06
    • 2014-12-29
    相关资源
    最近更新 更多