Estoy intentando reproducir un gif en una interfaz gráfica, el programa al parecer funciona bien, pero tengo 2 problemas: Aparecen un montón de errores al cerrar el programa y la reproducción del gif es muy rápida. Aquí está el código:
from tkinter import *
from PIL import Image, ImageTk, ImageSequence
root = Tk()
root.title("Entrenamiento")
root.resizable(FALSE, FALSE)
root.geometry('1000x700+300+50')
root.configure(background='#6dd5ed')
def play_gif():
img = Image.open("caracal-big.gif")
lbl = Label(root)
lbl.place(x=0, y=0)
for img in ImageSequence.Iterator(img):
img = img.resize((300,300))
img = ImageTk.PhotoImage(img)
lbl.config(image = img)
root.update()
root.after(0, play_gif)
btn = Button(root, text= "play", command= play_gif)
btn.place(x=500, y=300)
root.mainloop()
Estos son los errores que aparecen al cerrar el programa. Cabe mencionar que aparecen cuando se presiona el botón de "play" y luego se cierra el programa.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "c:\Users\USER\OneDrive\Documentos\Proyecto Integrador 1er Semestre\reproductor.py", line 38, in play_gif
img = ImageTk.PhotoImage(img)
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\PIL\ImageTk.py", line 140, in __init__
self.__photo = tkinter.PhotoImage(**kw)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 4103, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 4036, in __init__
master = _get_default_root('create image')
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 297, in _get_default_root
raise RuntimeError(f"Too early to {what}: no default root window")
RuntimeError: Too early to create image: no default root window
Exception ignored in: <function PhotoImage.__del__ at 0x000001B9D4525EA0>
Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\PIL\ImageTk.py", line 146, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'