如何在这些仪表板中停止一个文件并播放另一个文件?

时间:2019-01-30 07:46:28

标签: python python-3.x tkinter tkinter-canvas subproject

在代码中我需要一个帮助,当我单击测试2并在单击停止时再次启动它时,应该如何关闭测试1。

我已经在测试1,测试2中的代码中声明了两个按钮中的两个视频,当我单击test1时,它在第二次单击时播放了它,但它是一样的..问题是,当我播放测试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时应重新开始播放,然后再次单击任何按钮

1 个答案:

答案 0 :(得分:1)

主要问题是,只要您单击Test 1Test 2按钮,您的代码就会创建新线程。但是,旧线程仍在运行,因此有多个线程更新lbl,这会导致图像重叠。为了克服它,创建了一个线程来播放视频。 Test 1Test 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()