我正在尝试构建一个将图像序列显示为视频的 GUI。图像是 numpy 数组。当我尝试一次显示一张图像时,代码可以正常工作,但当我尝试将它们作为序列运行时,代码会崩溃。代码: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条经验 获得超5个赞
time.sleep
阻塞 的主线程tkinter
。您的代码将冻结 GUI,直到for
循环完成并且图像将显示为最后一个图像。
你需要使用该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
提交
取消