我正在嘗試構建一個將圖像序列顯示為視頻的 GUI。圖像是 numpy 數(shù)組。當我嘗試一次顯示一張圖像時,代碼可以正常工作,但當我嘗試將它們作為序列運行時,代碼會崩潰。代碼:from tkinter import *from scipy.io import loadmatfrom PIL import ImageTk, Imageimport timedata = loadmat('DepthFrames.mat')['DepthFrames'].squeeze(axis=0)print(data.shape)counter = 0root = Tk()image = ImageTk.PhotoImage(image = Image.fromarray(data[counter]))root.title("WUDU VIDEOS LABEL TOOL")myLabel = Label(root, image = image)myLabel.grid(row = 0)def changeImg(): global counter counter +=1 print(counter) image = ImageTk.PhotoImage(image = Image.fromarray(data[counter])) myLabel.configure(image = image) myLabel.image = imagedef playVideo(): for i in range(10): image = ImageTk.PhotoImage(image = Image.fromarray(data[i])) myLabel.configure(image = image) myLabel.image = image time.sleep(0.03333)my_Button = Button(text = "Play video",command = playVideo)my_Button.grid(row = 1)root.mainloop()
1 回答

梵蒂岡之花
TA貢獻1900條經(jīng)驗 獲得超5個贊
time.sleep
阻塞 的主線程tkinter
。您的代碼將凍結 GUI,直到for
循環(huán)完成并且圖像將顯示為最后一個圖像。
你需要使用該after
方法。像這樣的東西:
def playVideo(frame=0):
? ? try:
? ? ? ? image = ImageTk.PhotoImage(image = Image.fromarray(data[frame]))
? ? except IndexError:
? ? ? ? return
? ? myLabel.configure(image = image)
? ? myLabel.image = image
? ? root.after(33, playVideo, frame+1)
添加回答
舉報
0/150
提交
取消