self.tk.call(('image', 'create', imgtype, name,) + options) _tkinter.TclError: 无法识别图像文件中的数据“image1.png” - 无法加载png
我正在尝试使用 Python 和 Tkinter 制作一个应用程序,但我无法加载图像,或者具体来说是 png。相反,我收到了这个很长的错误:
File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4038, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "image1.png".
有人可以帮我解决这个问题吗?以下是可能导致错误的代码行:
cycle1IMG = tk.PhotoImage(file="image1.png")
def openNewWindow1():
newWindow = Toplevel(master)
newWindow.title("
I'm trying to make an app using Python and Tkinter but I'm not able to load an image, or png to be specific. Instead, I'm getting this long error:
File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4038, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "image1.png".
Can someone help me fix this? Here are the lines of code that might be causing the error:
cycle1IMG = tk.PhotoImage(file="image1.png")
def openNewWindow1():
newWindow = Toplevel(master)
newWindow.title("????Cycle 1")
newWindow.geometry("750x500")
Label(newWindow, text="Cycle 1", image= cycle1IMG).pack()
cycle1 =tk.Button(
root,
text="????Cycle 1",
command=openNewWindow1
)
cycle1.place(x=500,y=300)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据对该问题的评论,您似乎获取了一个 .webp 文件并将其重命名为 .png。这不会更改文件的内部内容,这就是 tkinter 报告错误的原因:您告诉它打开 .png 文件,但文件的内容不是 png 格式。
Tkinter 不支持 webp。您必须使用其他库来读取文件并将其转换为 tkinter 支持的格式(例如:
Based on a comment to the question, it appears that you took a .webp file and just renamed it to .png. This doesn't change the internal contents of the file, which is why tkinter reported the error: you told it to open a .png file but the contents of the file isn't in the png format.
Tkinter doesn't support webp. You'll have to use some other library to read the file and convert it to a format supported by tkinter (eg: pillow)