无法识别图像文件中的数据

无法识别图像文件中的数据

本文介绍了tkinter.TclError:无法识别图像文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在录制 Python 讲座,我认为老师使用的 Python 版本已经过时,他正在编写 tkinter 程序,但在复制它后,我不断收到此错误:

I'm currently in the middle of a recorded Python lecture and I think the version of python the teacher is using is outdated and he is writing a tkinter program but after copying it I keep getting this error:

Traceback (most recent call last):
  File "C:\Users\1234\AppData\Local\Programs\Python\Python36-32\Lecture Class 11.py", line 35, in <module>
    photo = PhotoImage(file="Me With My 5th Place.jpg")
  File "C:\Users\1234\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3539, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\1234\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3495, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "Me With My 5th Place.jpg"

这是我的代码:

from tkinter import *

class Application(Frame):
    def __init__(self, master):
        super(Application, self).__init__(master)
        self.grid()
        self.button_clicks = 0
        self.create_widgets()

    def create_widgets(self):
        self.label1 = Label(self)
        label1.image = photo
        label1.grid()
        self.button1 = Button(self)
        self.button1["text"] = "Click me!!!"
        self.button1["command"] = self.update_count
        self.button1.grid(row=1, column=1)
        self.button2 = Button(self)
        self.button2["text"] = "Reset click counter"
        self.button2["command"] = self.reset_count
        self.button.grid(row=3, column= 1)

    def update_count(self):
        self.button_clicks += 1
        self.button1["text"] = "Total clicks: " + str(self.button_clicks)

    def res_count(self):
        self.button_clicks = 0
        self.button["text"] = "I am reset. Click me!!!"

root = Tk()
root.title("CP101 click counter")
root.geometry("400x400")

photo = PhotoImage(file="Me With My 5th Place.jpg")

app = Application(root)

root.mainloop()

推荐答案

这很正常.Tkinter 尚不支持 .jpg 文件.要解决您想将文件转换为 .png、.gif 或类似格式或使用 Pillow 的问题 模块添加对 .jpg 文件的支持.

This is quite normal. Tkinter does not yet support .jpg files. To solve the problem you want to either to convert your file to .png, .gif or similar or use Pillow module to add support for .jpg files.

这篇关于tkinter.TclError:无法识别图像文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 08:43