python - 如何在 PYTHON 中特别是在 LINUX 中使用 TKINTER 在工具栏上插入自定义图标?

标签 python tkinter

我尝试使用以下代码执行相同的操作:

# A Python program to create root window with some options
from tkinter import *

# create top level window
root = Tk()

# set window title
root.title('Tkinter Demo')

# set window size
root.geometry('500x500')

# set window icon
root.wm_iconbitmap('PYTHON\Graphical User Interface[GUI]\pythonlogo.ico') # this line won't work for linux

# display window and wait for any events
root.mainloop()

我也尝试过:

# A Python program to create root window with some options
from tkinter import *

# create top level window
root = Tk()

# set window title
root.title('Tkinter Demo')

# set window size
root.geometry('500x500')

# set window icon
img = PhotoImage(file='PYTHON/Graphical User Interface[GUI]/pythonlogo.xbm')
root.tk.call('wm', 'iconphoto', root._w, img)

# display window and wait for any events
root.mainloop()

上面的代码在 Windows 中运行得很好,但对于 Linux,我使用了图像文件格式:.png、.jpeg、.gif、.ico、.xbm、.xpm,但它们都不起作用。

我知道不能使用.png、.jpeg。并且,.gif、.ico 格式仅适用于 Windows。我从几个论坛发现 .xpm 或 .xbm 格式工作得很好。但是,就我而言,情况并非如此。

我在使用 .xpm 和 .xpm 格式时遇到以下错误:

_tkinter.TclError: couldn't recognize data in image file "PYTHON/Graphical User Interface[GUI]/pythonlogo.xbm"

我很长一段时间以来一直在努力解决这个问题。 非常感谢任何针对该主题的指示或帮助。

谢谢!

最佳答案

您可以使用root.wm_iconphoto(True, img),其中img是从.png或.gif图像创建的PhotoImage文件。

这是该方法的文档字符串:

wm_iconphoto(default=False, *args) method of tkinter.Tk instance Sets the titlebar icon for this window based on the named photo images passed through args. If default is True, this is applied to all future created toplevels as well.

The data in the images is taken as a snapshot at the time of invocation. If the images are later changed, this is not reflected to the titlebar icons. Multiple images are accepted to allow different images sizes to be provided. The window manager may scale provided icons to an appropriate size.

On Windows, the images are packed into a Windows icon structure. This will override an icon specified to wm_iconbitmap, and vice versa.

On X, the images are arranged into the _NET_WM_ICON X property, which most modern window managers support. An icon specified by wm_iconbitmap may exist simultaneously.

On Macintosh, this currently does nothing.

在您的代码中:

from tkinter import *

# create top level window
root = Tk()

# set window title
root.title('Tkinter Demo')

# set window size
root.geometry('500x500')

# set window icon
img = PhotoImage(file='PYTHON/Graphical User Interface[GUI]/pythonlogo.png')
root.wm_iconphoto(True, img)

# display window and wait for any events
root.mainloop()

我在 Linux 中使用它并且它有效。

关于python - 如何在 PYTHON 中特别是在 LINUX 中使用 TKINTER 在工具栏上插入自定义图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64040158/

相关文章:

python - 如何根据字典列表中的特定值在单独的 Dataframe 列中创建列表?

python - WTForms 在下拉列表中显示外键字段的名称

python - 使用python将IR图像转换为RGB

python - 配置独立的小部件(Tkinter ttk、Python)

python - Tkinter 销毁并退出会卡住 Mac 上的根窗口

python - Tkinter:删除键盘双击预防

python - 使用Python将JSON存储到sqlite数据库中

python - 是否有与 RubyFlow 相当的 Python 新闻站点?

python - 100% 堆积条形图 - python (seaborn/matplotlib)

python - 如何关闭 Tkinter 窗口