简体   繁体   English

如何在 tkinter python 中为根窗口设置徽标

[英]how to set logo for root window in tkinter python

In the code below I am trying to set the logo for root window.在下面的代码中,我试图为根窗口设置徽标。 I tried .ico, .gif and .png file formats.我尝试了 .ico、.gif 和 .png 文件格式。 In case of .gif and .png images, logo appears only in taskbar but not on the root window.对于 .gif 和 .png 图像,徽标仅出现在任务栏中,而不出现在根窗口中。 And when I try .ico image, there is an error: tkinter.TclError: couldn't recognize data in image file "icon.ico".当我尝试 .ico 图像时,出现错误:tkinter.TclError:无法识别图像文件“icon.ico”中的数据。 I tried several methods but not a single one of them worked for me, out of which two methods are shown in code below.我尝试了几种方法,但没有一种对我有用,其中两种方法在下面的代码中显示。 Are there any specification for the logo images like image size, resolution, dimensions etc.. In case if it matters I am using linux machine.徽标图像是否有任何规范,例如图像大小、分辨率、尺寸等。如果重要,我使用的是 linux 机器。 CODE:代码:

from tkinter import *

root = Tk()
root.geometry('400x400')

# First method
root.tk.call('wm', 'iconphoto', root._w, PhotoImage(file="icon.ico"))

# Second method
root.iconbitmap(r'icon.png')

root.mainloop()

You don't need to use raw strings.您不需要使用原始字符串。 Make sure it's a legit .ico file and it's in the same directory as your python file.确保它是一个合法的 .ico 文件并且它与你的 python 文件在同一个目录中。 Then you can move it (.iconbitmap method) beneath your root window definition.然后您可以将它(.iconbitmap 方法)移动到根窗口定义下方。

root.iconbitmap('filename.ico')

If it doesn't work let me know.如果它不起作用,请告诉我。 It might be your setup or an OS specific error .这可能是您的设置或操作系统特定错误。 We'll try other ways.我们会尝试其他方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM