gpt4 book ai didi

Linux 错误上的 Python 3.6 tkinter 窗口图标

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:38:42 33 4
gpt4 key购买 nike

我正在学习 Python GUI Programming Cookbook 中的 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: error reading bitmap file Setting Application icon in my python Tk base application (On Ubuntu) tkinter.TclError: image "pyimage3" doesn't 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 库时如何更改窗口的图标。

最佳答案

我在 Linux Ubuntu (18.04) 上使用 .ico 文件无效。 (xbm、xpm、png、gif 也没有)。我也在使用 tkinter,因为 PySimpleGUI 是基于它的。

A base64 encoded gif image should work. At least it did for me:

with open('./assets/icon.gif', 'rb') as icon_gif:
icon_base64 = base64.b64encode(icon_gif.read())

# In PySimpleGUI:
sg.SetOptions(icon=icon_base64)

# In tkinter directly, probably:
win.iconbitmap(icon_base64)

我不确定 tkinter,因为我没有直接使用它。从 PySimpleGUI (v4.19.0) 的源代码来看,我认为应该这样做。如有必要,请纠正我。我会更新这个答案。

关于Linux 错误上的 Python 3.6 tkinter 窗口图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45361749/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com