我想在加載一個進度條時播放一個GIF并讓它回圈4秒。我嘗試了很多組合的解決方案,但都無濟于事。下面是我得到的最接近的答案。loadingAnimation()函式能很好地運行進度條,但我不能把GIF部分結合起來(M_95())。理想情況下,我希望GIF能在進度條加載的同時在螢屏中間播放,然后在完成后關閉GIF視窗。
import threading
import tkinter
root = tkinter.Tk()
frames = [PhotoImage(file='./myScripts/M-95.gif',format = 'gif -index %i' %(i)) for i in range(10)]
def M_95() 。
# 在320x320的tkinter視窗中播放GIF(檔案名=m95.gif)。
# 播放GIF與下面的加載影片同時進行。
# 播放后關閉tkinter視窗
def loadingAnimation(process)。
while process.is_alive() :
animation = ["[■□□□□□□□□□]"/span>,"[■□□□□□□□□]"/span>, "[■■■□□□□□□□]", "[■■■■□□□□□□]", "[■■■■■□□□□□]"。"[■■■■■■□□□□]", "[■■■■■■■□□] ", "[■■■■■■■■□□]"。"[■■■■■■■■■□]", "[■■■■■■■■■■]"]
for i in range(len(animation)):
sys.stdout.write("
| Loading..." animation[i % len(animation)])
sys.stdout.flush()
time.sleep(0.4)
loading_process = threading.Thread(target = M_95)
loading_process.start()
loadingAnimation(loading_process)
loading_process.join()
uj5u.com熱心網友回復:
首先不建議在tkinter應用程式的主執行緒中使用while回圈和time.sleep(),因為它將阻塞tkinter mainloop(),然后導致GUI沒有回應。
建議:
- 使用
.after()來顯示GIF影片,因為在一個執行緒中更新tkinter widgets是不安全的 - 在
loadingAnimation()上使用執行緒代替 。
import執行緒
import tkinter
import sys
import time
root = tkinter.Tk()
frames = [tkinter.PhotoImage(file='./myScripts/M-95.gif', format='gif -index %i'%(i)) for i in range(10)]
def center_window(win)。
win.wait_visibility() # make sure the window is ready.
x = (win.winfo_screenwidth() - win.winfo_width()) // 2
y = (win.winfo_screenheight() - win.winfo_height()) //2
win.geometry(f' {x} {y}'/span>)
def M_95(n=0。top=None, lbl=None)。)
# Play GIF (file name = m95.gif) in a 320x320 tkinter window.
# 播放GIF與下面的加載影片同時進行。
# 播放完畢后關閉tkinter視窗。
global process_is_alive # used in loadingAnimation()
delay = 4000 // len(frames) # make one cycle of animation around 4 secs
if n == 0:
root.withdraw()
top = tkinter.Toplevel()
lbl = tkinter.Label(top, image=frames[0] )
lbl.pack()
center_window(top)
process_is_alive = Trueif n < len(frames)-1:
lbl.config(image=frames[n])
lbl.after(delay, M_95, n 1, top, lbl)
else:
top.destroy()
root.deiconify()
process_is_alive = False: top.destroy()
def loadingAnimation()。
animation = ["[■□□□□□□□□□]"/span>,"[■□□□□□□□□]"/span>。"[■■■□□□□□□□]", "[■■■■□□□□□□]", "[■■■■■□□□□□]"。"[■■■■■■□□□□]", "[■■■■■■■□□] ", "[■■■■■■■■□□]"。"[■■■■■■■■■□]", "[■■■■■■■■■■]"]
i = 0]
while process_is_alive:
sys.stdout.write("
| Loading..." animation[i % len(animation)])
sys.stdout.flush()
time.sleep(0.4)
i = 1 1
M_95() # 開始GIF影片。
threading.Thread(target=loadingAnimation).start()
root.mainloop()
Update: animate GIF more than one cycle in around 4 secs:
def M_95(n=0, top=None, lbl=None)。)
# Play GIF (file name = m95.gif) in a 320x320 tkinter window.
# 播放GIF與下面的加載影片同時進行。
# 播放完畢后關閉tkinter視窗。
global process_is_alive
num_cycles = 2
count = len(frames) * num_cycles
delay = 4000 // count # 在4秒左右做出所需的影片周期。
if n == 0:
root.withdraw()
top = tkinter.Toplevel()
lbl = tkinter.Label(top, image=frames[0] )
lbl.pack()
center_window(top)
process_is_alive = True1, top, lbl)
elif n < count-1:
lbl.config(image=frames[n%len(frames)] )
lbl.after(delay, M_95, n 1, top, lbl)
else:
top.destroy()
root.destroy()
process_is_alive = False: top.destroy()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/319461.html
標籤:
