我正在嘗試構(gòu)建一個(gè)將圖像序列顯示為視頻的 GUI。圖像是 numpy 數(shù)組。當(dāng)我嘗試一次顯示一張圖像時(shí),代碼可以正常工作,但當(dāng)我嘗試將它們作為序列運(yùn)行時(shí),代碼會(huì)崩潰。代碼: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貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊
time.sleep
阻塞 的主線程tkinter
。您的代碼將凍結(jié) GUI,直到for
循環(huán)完成并且圖像將顯示為最后一個(gè)圖像。
你需要使用該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)
添加回答
舉報(bào)
0/150
提交
取消