如何使用Tkinter设置窗口图标?
我正在尝试设置一个窗口图标,这样它就会出现在我正在做的词典的名称旁边。运行代码时,出现以下问题:如何使用Tkinter设置窗口图标?,tkinter,icons,macos-catalina,python-3.8,.ico,Tkinter,Icons,Macos Catalina,Python 3.8,.ico,我正在尝试设置一个窗口图标,这样它就会出现在我正在做的词典的名称旁边。运行代码时,出现以下问题: Traceback (most recent call last): File "/Users/sergioley-languren/Latin_app/windows.py", line 20, in <module> window.iconphoto(False, tk.PhotoImage(file='/Users/sergioley-languren/home/Lat
Traceback (most recent call last):
File "/Users/sergioley-languren/Latin_app/windows.py", line 20, in <module>
window.iconphoto(False, tk.PhotoImage(file='/Users/sergioley-languren/home/Latin_app/Logo.jpeg'))
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 4061, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 4006, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "/Users/sergioley-languren/Latin_app/Logo.jpeg"
有人能告诉我怎么修吗?
(如果有帮助,我正在使用MacOs Catilina。)
tkinter.PhotoImage
仅支持GM、PPM、GIF、PNG
图像。您的图像是JPEG
,肯定会引发异常
有一些解决方案,但直接的方法是将图像扩展名更改为ico
,并使用iconbitmap()
这将在当前路径中生成一个新的ico
图像
然后您可以直接使用iconbitmap('Icon.ico')。
tkinter.PhotoImage
仅支持GM、PPM、GIF、PNG
图像。您的图像是JPEG
,肯定会引发异常
有一些解决方案,但直接的方法是将图像扩展名更改为ico
,并使用iconbitmap()
这将在当前路径中生成一个新的ico
图像
然后您可以直接使用iconbitmap('Icon.ico')。这可以为Tkinter应用程序设置图标
from tkinter import *
window =Tk()
##set title for window
window.title("Tkinter App")
##set window icon
window.iconphoto(False,PhotoImage(file='logo.png'))
window.mainloop()
这可用于设置Tkinter应用程序的图标
from tkinter import *
window =Tk()
##set title for window
window.title("Tkinter App")
##set window icon
window.iconphoto(False,PhotoImage(file='logo.png'))
window.mainloop()
Python找不到PIL它在我尝试执行第一行时给了我一个错误。@SergioLey Languren,所以你应该使用
pip install pillow
来安装它。我现在遇到了这个错误:FileNotFoundError:[Errno 2]没有这样的文件或目录:“Logo.jpeg”
。如何修复此问题?请检查路径。您的路径可能应该是“Logo.jpg”。它不是,我更改了它,但现在这是当前错误:FileNotFoundError:[Errno 2]没有这样的文件或目录:“Logo.jpg”
Python找不到PIL它在我尝试执行第一行时给了我一个错误。@SergioLey Languren所以你应该使用pip-install-pillow
来安装它。我现在遇到这个错误:FileNotFoundError:[Errno 2]没有这样的文件或目录:“Logo.jpeg”
。如何修复此问题?请检查路径。您的路径可能应该是“Logo.jpg”。它不是,我更改了它,但现在这是当前的错误:FileNotFoundError:[Errno 2]没有这样的文件或目录:“Logo.jpg”
from tkinter import *
window =Tk()
##set title for window
window.title("Tkinter App")
##set window icon
window.iconphoto(False,PhotoImage(file='logo.png'))
window.mainloop()