python代码在电脑上能跑,在开发板上会报这个错误,库我已经安好,这可能是什么原因,怎么修改?
Traceback (most recent call last):
File "33.py", line 79, in <module>
app = App(frame, "{}.gif".format(i))
File "33.py", line 32, in __init__
self.gif = AnimatedGif(gif_file)
File "33.py", line 12, in __init__
self.load_frames()
File "33.py", line 18, in load_frames
self.frames.append(ImageTk.PhotoImage(im.copy()))
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 1108, in copy
self.load()
File "/usr/lib/python3/dist-packages/PIL/ImageFile.py", line 215, in load
decoder.setimage(self.im, extents)
ValueError: tile cannot extend outside image
源代码:
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
class AnimatedGif:
def __init__(self, filename):
self.filename = filename
self.frames = []
self.cur_frame = 0
self.num_frames = 0
self.load_frames()
def load_frames(self):
im = Image.open(self.filename)
try:
while True:
self.frames.append(ImageTk.PhotoImage(im.copy()))
im.seek(len(self.frames)) # skip to next frame
except EOFError: # we're done
pass
self.num_frames = len(self.frames)
def next_frame(self):
self.cur_frame = (self.cur_frame + 1) % self.num_frames
return self.frames[self.cur_frame]
class App:
def __init__(self, master, gif_file):
self.master = master
self.gif = AnimatedGif(gif_file)
self.playing = False
self.create_widgets()
def create_widgets(self):
self.canvas = tk.Canvas(self.master)
self.canvas.pack(fill=tk.BOTH, expand=True)
self.play_button = tk.Button(self.master, text="播放", command=self.toggle_play)
self.play_button.pack(side=tk.BOTTOM, padx=10, pady=10)
self.label = tk.Label(self.master, text="这是一个界面")
self.label.pack(padx=10, pady=10)
def toggle_play(self):
if self.playing:
self.playing = False
self.play_button.configure(text="重新播放")
else:
self.playing = True
self.play_button.configure(text="暂停")
self.animate_frames()
def animate_frames(self):
if not self.playing:
return
frame = self.gif.next_frame()
self.canvas.delete(tk.ALL)
self.canvas.create_image(0, 0, image=frame, anchor=tk.NW)
self.master.after(100, self.animate_frames)
if __name__ == "__main__":
root = tk.Tk()
root.geometry("650x600+200+100")
# Create a ttk.Notebook widget
notebook = ttk.Notebook(root)
notebook.pack(fill=tk.BOTH, expand=True)
name1='1'
name2='2'
name3='3'
# Create three tabs with different content
for i in range(1, 4):
# Create a new App object for each tab
frame = ttk.Frame(notebook)
app = App(frame, "{}.gif".format(i))
app.canvas.pack(fill=tk.BOTH, expand=True)
app.label.configure(text='name{}'.format(i))
# Add the tab to the notebook
notebook.add(frame, text="界面 {}".format(i))
root.mainloop()
python调试三板斧 https://ask.csdn.net/questions/7908322/54130133
借助print("pause")解决了我的问题
究其原因是我用的.gif有问题,换成其他的.gif就好了