tkinter想实现动态图出现问题

from tkinter import *

from PIL import Image, ImageTk, ImageSequence
import time

root = Tk()
root.geometry("300x300")
canvas = Canvas(root,width=300, height=300,bg='white')
canvas.pack()


#分解gif并逐帧显示
def pick(event):
    global a,flag
    while 1:
        im = Image.open('1.gif')
        # GIF图片流的迭代器
        iter = ImageSequence.Iterator(im)
        #frame就是gif的每一帧,转换一下格式就能显示了
        for frame in iter:
            pic=ImageTk.PhotoImage(frame)
            canvas.create_image((100,150), image=pic)
            time.sleep(0.1)
            root.update_idletasks()  #刷新
            root.update()

canvas.bind("<Enter>",pick)  #这个事件是鼠标进入组件,用什么事件不重要,这里只是演示
root.mainloop()

错误警告:Exception in Tkinter callback
Traceback (most recent call last):
File "C:UsersGaoXXAppDataLocalProgramsPythonPython38libtkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "E:/python/python2/2.py", line 21, in pick
pic=ImageTk.PhotoImage(frame)
File "C:UsersGaoXXAppDataLocalProgramsPythonPython38libsite-packagesPILImageTk.py", line 112, in __init__
self.__photo = tkinter.PhotoImage(**kw)
File "C:UsersGaoXXAppDataLocalProgramsPythonPython38libtkinter\__init__.py", line 4061, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:UsersGaoXXAppDataLocalProgramsPythonPython38libtkinter\__init__.py", line 3994, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image
Exception ignored in: Traceback (most recent call last):
File "C:UsersGaoXXAppDataLocalProgramsPythonPython38libsite-packagesPILImageTk.py", line 118, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

Process finished with exit code 0
看不太懂求救援

阅读 2.8k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题