我正在尝试创建一个小类来表示有关我希望软件处理的各种 GIF 的重要信息。这是我的代码:
from PIL import Image
import tkinter as tk
class ListifiedGif:
"""Class containing list of frames and number of frames from GIF."""
def __init__(self, filename):
gif = Image.open(filename)
self.frame_count = gif.n_frames
self.frame_list = [tk.PhotoImage(file = filename,
format = f'gif -index {i}')
for i in range(gif.n_frames)]
当我在文件末尾插入以下内容时,出现错误。
testimg = ListifiedGif('E:\\Development\\desktop\\img\\walking_negative.gif')
print(testimg.frame_count)
print(testimg.frame_list)
...这是错误:
Traceback (most recent call last):
File "<module1>", line 13, in <module>
File "<module1>", line 9, in __init__
File "<module1>", line 9, in <listcomp>
File "C:\Program Files\Python39\lib\tkinter\__init__.py", line 4064, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Program Files\Python39\lib\tkinter\__init__.py", line 3997, in __init__
master = _get_default_root('create image')
File "C:\Program Files\Python39\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
为什么必须声明根窗口才能创建图像?有没有办法解决这个问题,以便我可以简单地返回帧列表(作为来自 tkinter 的一系列 PhotoImages),而不必担心将使用图像的窗口看起来像什么?
在此先感谢您的时间。