2017-05-08 131 views
0

我使用的Tkinter從以下網頁動畫使用指導GIF: Tkinter animation will not work的Tkinter未能動畫GIF

但是我不能得到的建議工作。現在,我有這樣的事情

import tkinter as tk 
root = tk.Tk() 
frames = [tk.PhotoImage(file = '/home/quantik/Pictures/dice.gif',format="gif -index %i" %(i)) for i in range(5)] 
def animate(n): 
    if (n < len(frames)): 
     label.configure(image=frames[n]) 
     n+=1 
     root.after(300, animate(n)) 

label = tk.Label(root) 
label.pack() 
root.after(0, animate(0)) 
root.mainloop() 

但是它只是顯示最後一個圖像中frames我試過幾種方法,但我不斷收到相同的結果。有沒有人有任何建議,爲什麼這可能會發生?

回答

2

此代碼:

root.after(300, animate(n)) 

是完全一樣的,因爲這代碼:

result = animate(n) 
root.after(300, result) 

注意發生了什麼?你不想調用這個函數,你想告訴after稍後調用它。你是這樣做的:

root.after(300, animate, n) 

這告訴root.after調用animate功能後300毫秒,傳遞的n值作爲參數傳遞給函數。

+0

This Works!我想我沒有完全理解'after'文檔,我認爲它會等待300毫秒,然後調用'animate(args)'。謝謝! – quantik