我有一个动画gif永远在while循环中运行,但我只希望gif运行一次。
我尝试删除while循环,但后来我没有定义last_frame。
import tkinter as tk
def animate(frame_number, last_frame, label, framelist,window):
#if the frame number is bigger than the last frame
if frame_number > last_frame:
#set frame number to 0
frame_number = 0
#configure the impage in to a list
label.config(image=framelist[frame_number])
#change the frame after how many seconds
window.after(1500, animate, frame_number+1, last_frame, label, framelist,window)
#make mainwindow
window = tk.Tk()
# List to hold all the frames
framelist = []
# Frame index set to 0
frame_index = 0
#if true
while True:
try:
#Read a frame from GIF file
part = 'gif -index {}'.format(frame_index)
#import the gif
frame = tk.PhotoImage(file='giphy.gif', format=part)
except:
# Save index for last frame
last_frame = frame_index - 1
# Will break when GIF index is reached
break
# Next frame index
framelist.append(frame)
#add one to make the gif going
frame_index += 1
#put the gif into a label
label = tk.Label(window, bg='black')
label.grid(column=1, row=7)
# Start animation
animate(0, last_frame, label, framelist, window)
#window size
window.geometry("600x600")#winwindow.geometry("600x600")
#end of window
window.mainloop()
我只希望gif一次运行。 预先感谢。
答案 0 :(得分:0)
通常建议不要与mainloop()
处理的tkinter一起使用循环。
在这里阅读:https://stackoverflow.com/a/24131612/10520989
最好的解决方案是使用.after()
import tkinter as tk
master = tk.Tk()
def my_mainloop():
print "Hello World!"
master.after(1000, my_mainloop)
master.after(1000, my_mainloop)
master.mainloop()