就是我贴了gif 图 在canvas上 然后可以正常运行 但是关闭对话框 会出现代码
def Leavehome(self):
Creatending1 = tk.Toplevel()
Creatending1.geometry('820x500')
Creatending1.title('Ending')
Creatending1.resizable(False, False)
canvas = Canvas(Creatending1, width=500, height=300, bg='white')
canvas.pack()
endingtext1 = " You give up this task,\n and then you quit your job at home.\nAlthough you lost your job,\nyou came to a new town and settled here.\n" \
"Later, you have a beautiful wife and have 3 children.\nFinally, at the age of 85, \nyou passed away suddenly and\n long in the company of your family."
# \n is enter \t is space
self.endtext1 = Label(Creatending1, text=endingtext1
, width=50, height=300, font=('Times New Roma', 10), fg="black").pack(side=tk.BOTTOM)
def pick(self):
global a, flag
while 1:
im = Image.open('images/ending1.gif')
# GIF图片流的迭代器
iter = ImageSequence.Iterator(im)
# frame就是gif的每一帧,转换一下格式就能显示了
for i in iter:
pic = ImageTk.PhotoImage(i)
canvas.create_image(250,150, image=pic)
time.sleep(0.1)
Creatending1.after(15000, Creatending1.destroy)
Creatending1.update_idletasks() # 刷新
Creatending1.update()
canvas.bind("<Enter>", pick)
Creatending1.mainloop()
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\tkinter_init.py", line 1705, in call
return self.func(*args)
File "C:/Users/Administrator/Desktop/python program/python coursework/courseworkPart2/coursework2GUI.py", line 231, in pick
canvas.create_image(250,150, image=pic )
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\tkinter_init.py", line 2489, in create_image
return self.create('image', args, kw)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\tkinter_init.py", line 2480, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: invalid command name ".!toplevel.!canvas"
Process finished with exit code 0
只想不报错
请问题主解决了吗,我也遇到同样的问题了
使用pil生成的tkinter图片类是一个临时变量,尝试在创建pic后使用如下代码:
canvas.pic1=pic
然后再使用canvas.pic1替代后面参数中的pic