我的程序使用VLC绑定来播放视频,而使用time.sleep
函数等待视频结束后再播放下一个视频。它将在一个循环中播放,直到用户按下按钮,并且只有在视频完成后才会退出。在
我使用Threading
来处理视频播放,因为time.sleep
从root
窗口夺走了控制权,并且需要按钮来停止视频。线程解决了这个问题。在
我唯一的问题是视频需要嵌入窗口。此窗口充当主程序的覆盖层(它提供完全不同的功能),并且当下面有更新时(这是将视频直接嵌入root
窗口时)从视频中移除闪烁。在
我知道Threading
不能处理Tkinter
所生成的任何GUI内容,所以我尝试了解决这个问题。它在很大程度上起作用,只是在线程退出时或之前销毁窗口时,我遇到了麻烦。在
下面是一些示例代码,它打开一个窗口,然后不断地进行计数并打印结果,直到您告诉它停止为止(在实际的程序中,它在Toplevel
窗口中播放视频)。有一个额外的按钮来破坏窗口。如何让Destroy
通过线程(而不是按钮)运行?有可能吗?在
from tkinter import *
import os
import threading
import time
root = Tk()
class Player(Frame):
def __init__(self, parent=None, **kw):
self.Playing = 0
def Play(self):
self.x = 0
while self.Playing == 1:
print(self.x)
self.x = self.x + 1
time.sleep(3)
if self.Stopping == 1:
self.Playing = 0
sys.exit()
def Go(self):
if self.Playing == 0:
self.Stopping = 0
self.Playing = 1
self.vidcanvas = Toplevel()
self.vidcanvas.configure(bg='black')
self.vidcanvas.geometry('200x200')
self.th1 = threading.Thread(target=self.Play)
self.th1.start()
def Destroy(self):
self.Stopping = 1
self.vidcanvas.destroy()
def Stop(self):
self.Stopping = 1
ply = Player(root)
go = Button(root, text='Play', width=25, command=ply.Go)
go.grid()
stop = Button(root, text='Stop at End of Count', width=25, command=ply.Stop)
stop.grid()
stop = Button(root, text='Kill Window', width=25, command=ply.Destroy)
stop.grid()
root.mainloop()
目前没有回答
相关问题 更多 >
编程相关推荐