python - 如何在这些仪表板中停止一个文件并播放另一个文件?
问题描述
我在我的代码中需要一个帮助,当我单击测试 2 以在单击停止时再次启动它时,我应该怎么做才能关闭测试 1。
我在测试 1 的代码中的两个按钮中声明了两个视频,测试 2 当我单击测试 1 时它会播放它,当我单击 2 时它会执行相同的操作,但是.. 问题是当我播放测试 1 并在之后单击测试 2它同时播放一个和另一个顶部。当我单击停止并再次单击测试 1 时,它不会启动。
import cv2
from tkinter import *
from PIL import Image, ImageTk
import threading
from tkinter.filedialog import askopenfilename
def resize(image):
im = image
new_siz = siz
im.thumbnail(new_siz, Image.ANTIALIAS)
return im
def size(event):
global siz
if siz == screenWH:
siz = (600, 200)
else:
siz = screenWH
win.state('zoomed')
print ("size is:"), siz
def view_frame_video():
vc = cv2.VideoCapture('C:\\Users\\Devendra\\Desktop\\VID_20190125_172553.mp4')
if vc.isOpened():
rval , frame = vc.read()
else:
rval = False
while rval:
rval, frame = vc.read()
img =Image.fromarray(frame)
img = resize(img)
imgtk = ImageTk.PhotoImage(img)
lbl.config(image=imgtk)
lbl.img = imgtk
if stop == True:
vc.release()
break #stop the loop thus stops updating the label and reading imagge frames
cv2.waitKey(1)
vc.release()
def view_frame_video2():
vc = cv2.VideoCapture('C:\\Users\\Devendra\\Desktop\\VID_20190129_162657.mp4')
if vc.isOpened():
rval , frame = vc.read()
else:
rval = False
while rval:
rval, frame = vc.read()
img =Image.fromarray(frame)
img = resize(img)
imgtk = ImageTk.PhotoImage(img)
lbl.config(image=imgtk)
lbl.img = imgtk
if stop == True:
vc.release()
break #stop the loop thus stops updating the label and reading imagge frames
cv2.waitKey(1)
vc.release()
def stop_():
global stop
stop = True
def play():
stop = False
t = threading.Thread(target=view_frame_video)
t.start()
def play2():
stop = False
t = threading.Thread(target=view_frame_video2)
t.start()
win = Tk()
win.title('Data Logger')
win.geometry('1300x650')
stop = None
screenWH = (win.winfo_screenwidth(), win.winfo_screenheight())
siz = (600, 600)
Label(text='Prosthetic Hand Testing Software',font = ('times',22), fg = 'Red',width = 104, height = 2).pack()
def import_csv_data():
global v
csv_file_path = askopenfilename()
print(csv_file_path)
v.set(csv_file_path)
Label(win, text='File Path',font = ('arial' ,10), fg = 'black', width = 104, height = 2).pack()
v = StringVar()
entry = Entry(textvariable = v)
entry.pack()
Button(text='Browse Data Set',command=import_csv_data).pack()
frm_ = Frame(bg='black')
frm_.pack()
lbl = Label(frm_, bg='black')
lbl.pack(expand=True)
lbl.bind('<Double-Button-1>', size)
buttonframe = Frame(win)
buttonframe.pack(side=BOTTOM)
Button(text='Test 1', command = play,fg="Green", height=2, width=20).pack(side=LEFT)
Button(text='Test 2', command = play2,fg="Green", height=2, width=20).pack(side=LEFT)#,padx=21, pady=18)
Button(buttonframe,text='Stop', command = stop_,fg="Orange", height=2, width=20).pack(side=LEFT,padx=20, pady=18)
Button(buttonframe,text='Exit',fg="Red",command=win.destroy,height=2, width=20).pack(side=LEFT,padx=20, pady=18)
win.mainloop()
当我在测试 1 之后单击测试 2 时应该关闭测试 1 并播放测试 2 当我停止它并再次单击任何按钮时应该重新开始播放测试 1,2
解决方案
主要问题是每当您单击Test 1
或Test 2
按钮时,您的代码都会创建新线程。然而,旧线程仍在运行,因此有不止一个线程在更新lbl
导致图像重叠的线程。为了克服它,创建了一个线程来播放视频。和Test 1
按钮Test 2
用于选择哪个视频文件作为源。以下是根据您的代码修改(和简化)的代码:
import cv2
from tkinter import *
from PIL import Image, ImageTk
import threading
def play_video():
while True:
try:
if vc.isOpened():
rval, frame = vc.read()
if rval:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(frame)
img.thumbnail(siz, Image.ANTIALIAS)
imgtk = ImageTk.PhotoImage(img)
lbl.config(image=imgtk)
lbl.img = imgtk
except:
pass
def play():
try:
vc.open('C:\\Users\\Devendra\\Desktop\\VID_20190125_172553.mp4')
except:
pass
def play2():
try:
vc.open('C:\\Users\\Devendra\\Desktop\\VID_20190129_162657.mp4')
except:
pass
def stop_():
try:
vc.release()
except:
pass
win = Tk()
win.title('Data Logger')
win.geometry('1300x650')
siz = (600, 400)
Label(text='Prosthetic Hand Testing Software', font=('times',22), fg='Red', width=104, height=2).pack()
lbl = Label(bg='black', image=ImageTk.PhotoImage(Image.new('RGB', siz)))
lbl.pack(expand=True)
buttonframe = Frame(win)
buttonframe.pack(side=BOTTOM)
Button(buttonframe, text='Test 1', command=play, fg="Green", height=2, width=20).pack(side=LEFT)
Button(buttonframe, text='Test 2', command=play2, fg="Green", height=2, width=20).pack(side=LEFT)
Button(buttonframe, text='Stop', command=stop_, fg="Orange", height=2, width=20).pack(side=LEFT, padx=20, pady=18)
Button(buttonframe, text='Exit', command=win.destroy, fg="red", height=2, width=20).pack(side=LEFT, padx=20, pady=18)
vc = cv2.VideoCapture()
# create and start the video playing thread
t = threading.Thread(target=play_video)
t.setDaemon(True)
t.start()
win.mainloop()
推荐阅读
- rest - Magento1.9 Rest Api 过滤器
- sql - 记录准确日期和时间的列
- android - Android:TextInputLayouts 总是有一个错误图标
- amazon-web-services - 有没有办法在不同的 ec2 实例中并行运行测试用例(例如,在三台 AWS 机器上运行一组 90 个用例)
- javascript - 链接在安装了 Fancybox 的 iOS 上不起作用(Wordpress)
- winapi - Windows 10 主题更改的通知/订阅方法
- bash - 如何查找文件中是否存在行并使用 awk 添加具有文件名的列?
- xml - 在 Crystal Reports 2016 中使用自定义 XSD
- d3.js - 由于目标被 D3 js 视为被动,因此无法在被动事件侦听器中阻止默认值
- r - 如何计算不在列表中的日期