当我尝试添加图像并将其设置为我的GUI窗口徽标时,它会给我这些错误
Traceback (most recent call last):
File "C:UsersMeina JiaPycharmProjectsguwindowmain.py", line 7, in <module>
icon = PhotoImage(file='logo.jpg')
File "C:UsersMeina JiaAppDataLocalProgramsPythonPython310libtkinter__init__.py", line 4093, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:UsersMeina JiaAppDataLocalProgramsPythonPython310libtkinter__init__.py", line 4038, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "logo.jpg"
Process finished with exit code 1
我已经使用代码更改了文件类型。
from tkinter import *
window = Tk()
window.geometry("420x420")
window.title("Backrooms in A Nutshell")
icon = PhotoImage(file='logo.jpg')
window.iconphoto(True, icon)
window.mainloop()
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "logo.jpg"
的意思。您不能将图像保存为并重新命名它。现在有办法解决这个问题。最好使用png
格式。
为此,您将需要Pillow库。
import tkinter as tk
from PIL import ImageTk, Image
window = tk.Tk()
window.geometry("420x420")
window.title("Backrooms in A Nutshell")
icon = ImageTk.PhotoImage(Image.open('logo.jpg'))
window.iconphoto(True, icon)
window.mainloop()
注意:直接导入库是一个好习惯:运行
import tkinter
不是
from tkinter import *