作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个程序可以在tkinter窗口中显示视频。当我想关闭窗口时,我停止了视频线程。但这并没有停止。有四个功能:
def start_video(self):
if self.video is not None:
self.video_thread_stopped = False
try:
if not self.video_thread.is_alive():
self.video_thread = threading.Thread(target=self.video_stream)
self.video_thread.start()
except AttributeError:
if self.video_thread is None:
self.video_thread = threading.Thread(target=self.video_stream)
self.video_thread.start()
start_video函数启动视频线程
def video_stream(self):
_, frame = self.video.read()
str_time = time.time()
while frame is not None and getattr(self.video_thread, "running", True):
self.current_frame += 1
# resize and set image to label
frame = cv2.resize(frame, self.video_frame_size)
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
imgtk = ImageTk.PhotoImage(image=Image.fromarray(cv2image))
self.video_label.config(image=imgtk)
self.video_label.image = imgtk
# waiting for frame rate
while time.time() - str_time < (1 / self.fps):
pass
str_time = time.time()
# reading next frame
_, frame = self.video.read()
print("exited from loop")
video_stream是线程函数
def pause_video(self):
if self.video_loaded:
self.video_thread_stopped = True
if self.video_thread is not None:
# stop video
self.video_thread.running = False
print('before join')
start_time = time.time()
while self.video_thread.is_alive():
if time.time() - start_time > 1:
break
print("after while")
self.video_thread.join()
print('after join')
pause_video必须终止线程并停止流视频
def on_window_close(self):
self.pause_video()
print("thread stopped")
self.root.destroy()
on_window_close必须在关闭tk窗口之前停止线程
root.protocol("WM_DELETE_WINDOW", w.on_window_close)
因此,当我启动视频线程并按tk窗口上的关闭按钮时,线程不会停止。这是终端输出
before join
after while
有人可以帮我一下,为什么不能停止video_thread。谢谢!
最佳答案
我找到了解决方案。
在start_video方法中,您必须创建video_thread守护程序:
def start_video(self):
if self.video is not None:
self.video_thread_stopped = False
try:
if not self.video_thread.is_alive():
self.video_thread = threading.Thread(target=self.video_stream)
self.video_thread.daemon = 1
# start audio
self.audio_class.start(self.current_frame, self.total_video_frames)
# start thread
self.video_thread.start()
except AttributeError:
if self.video_thread is None:
self.video_thread = threading.Thread(target=self.video_stream)
self.video_thread.daemon = 1
# start audio
self.audio_class.start(self.current_frame, self.total_video_frames)
# start thread
self.video_thread.start()
在pause_video方法中,您需要使用video_thread.join()删除行
def pause_video(self):
if self.video_loaded:
self.video_thread_stopped = True
if self.video_thread is not None:
# stop audio
# stop video
self.video_thread.running = False
print("video stopped")
self.audio_class.stop()
print("audio stopped")
# print('before join')
start_time = time.time()
while self.video_thread.is_alive():
# print("waiting for none")
if time.time() - start_time > 1:
# self.video_thread = None
break
关于python - 我想停止线程时不停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64459568/
我是一名优秀的程序员,十分优秀!