python - 在 Tkinter 中设置图标

标签 python python-2.7 tkinter windows-7 icons

<分区>

我想用我自己的替换 Tkinter 图标,但其他问题的答案无济于事。在同一文件夹中,我将我的脚本和使用 Paint 创建的图像保存为 icon.png。这是脚本:

from Tkinter import *

root = Tk()

icon = PhotoImage(file='icon.png')
root.tk.call('wm', 'iconphoto', root._w, icon)

root.mainloop()

这个错误是由 root.tk.call 方法引起的:

TclError: couldn't recognize data in image file "icon.png"

最佳答案

错误实际上在 icon = PhotoImage... 行中,在 root.tk.call... 之上 - 它试图将文件的数据读入PhotoImage 对象并失败。

Tkinter 本身不支持 PNG 文件类型。您需要使用受支持的文件类型,例如 GIF。如果您想从包括 PNG 在内的多种文件类型中进行选择,我建议安装 Pillow ,PIL(“Python 图像库”)的最新分支。但是,最简单的方法可能是再次在“画图”中打开图像,这次将其另存为 GIF,然后使用它。

关于python - 在 Tkinter 中设置图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34961020/

相关文章:

python-2.7 - 如何在 PyGraphviz 中创建重复节点?

python - TkInter ListBox 和 .format 的使用

python - PyQt 相当于 tkinter 的 Canvas 是什么?

Python Tkinter : Canvas scrolling with MouseWheel

python - 如何从游戏返回主菜单

python - 为从文件中读取的行分配名称

python - 关于在 matplotlib 中调整边距

python - 应用具有两个不同列表的公式

python - 如何从 Python 中的字符串中删除所有空格

Python:如何切断字符串中超过2个相等字符的序列