我正在从《Python GUI 编程手册》学习 Python GUI。某项任务要求我通过将以下代码添加到我的配方中来更改窗口图标:
# Change the main windows icon
win.iconbitmap(r'C:\Python34\DLLs\pyc.ico')
由于我使用的是Linux,所以我必须将路径更改为/home/user/test.ico
。阅读类似问题后,我了解到.ico
仅适用于 Windows。我试过.gif
但这也行不通。我尝试过的现有 SO 文章:tkinter TclError:读取位图文件时出错 https://stackoverflow.com/questions/11176638/tkinter-tclerror-error-reading-bitmap-file
在我的 python Tk 基础应用程序中设置应用程序图标(在 Ubuntu 上) https://stackoverflow.com/questions/16081201/setting-application-icon-in-my-python-tk-base-application-on-ubuntu
tkinter.TclError:图像“pyimage3”不存在 https://stackoverflow.com/questions/20251161/tkinter-tclerror-image-pyimage3-doesnt-exist
所有这三个都没有帮助。我收到以下每个错误:
In [3]: runfile('/home/bhedia/untitled1.py', wdir='/home/bhedia')
Traceback (most recent call last):
File "<ipython-input-3-17a671578909>", line 1, in <module>
runfile('/home/bhedia/untitled1.py', wdir='/home/bhedia')
File "/home/bhedia/anaconda3/envs/mitx/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile
execfile(filename, namespace)
File "/home/bhedia/anaconda3/envs/mitx/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/bhedia/untitled1.py", line 58, in <module>
img = tk.PhotoImage(file='test.ico')
File "/home/bhedia/anaconda3/envs/mitx/lib/python3.5/tkinter/__init__.py", line 3403, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/home/bhedia/anaconda3/envs/mitx/lib/python3.5/tkinter/__init__.py", line 3359, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: couldn't recognize data in image file "test.ico"
In [4]: runfile('/home/bhedia/untitled1.py', wdir='/home/bhedia')
Traceback (most recent call last):
File "<ipython-input-4-17a671578909>", line 1, in <module>
runfile('/home/bhedia/untitled1.py', wdir='/home/bhedia')
File "/home/bhedia/anaconda3/envs/mitx/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile
execfile(filename, namespace)
File "/home/bhedia/anaconda3/envs/mitx/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/bhedia/untitled1.py", line 59, in <module>
root.tk.call('wm','iconphoto',root._w,img)
TclError: can't use "pyimage2" as iconphoto: not a photo image
因此,我不确定在 Linux 上使用 Tkinter 库时如何更改 Windows 图标。