Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在这些仪表板中停止一个文件并播放另一个文件?_Python_Python 3.x_Tkinter_Tkinter Canvas_Subproject - Fatal编程技术网

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

Python 如何在这些仪表板中停止一个文件并播放另一个文件?,python,python-3.x,tkinter,tkinter-canvas,subproject,Python,Python 3.x,Tkinter,Tkinter Canvas,Subproject,我需要在我的代码中提供一个帮助,当我单击测试2时我应该如何关闭测试1,当单击停止时再次启动它 我在测试1的代码中声明了两个视频二合一按钮,当我点击测试1时,测试2会播放它,当我点击第二个按钮时,它也会播放,但是。。问题是,当我播放测试1并单击测试2之后,它会同时播放一个和另一个顶部。当我点击停止并再次点击测试1时,它不会启动 import cv2 from tkinter import * from PIL import Image, ImageTk import threading from

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

我在测试1的代码中声明了两个视频二合一按钮,当我点击测试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()
导入cv2
从tkinter进口*
从PIL导入图像,ImageTk
导入线程
从tkinter.filedialog导入askopenfilename
def调整大小(图像):
im=图像
新尺寸=尺寸
im.缩略图(新的大小、图像、反别名)
返回即时消息
def大小(事件):
全球规模
如果siz==screenWH:
siz=(600200)
其他:
siz=screenWH
win.state('缩放')
打印(“大小:”),大小
def view_frame_video():
vc=cv2.VideoCapture('C:\\Users\\Devendra\\Desktop\\VID\u 20190125\u 172553.mp4')
如果vc.isopend():
rval,frame=vc.read()
其他:
rval=False
而rval:
rval,frame=vc.read()
img=Image.fromarray(帧)
img=调整大小(img)
imgtk=ImageTk.照片图像(img)
lbl.config(image=imgtk)
lbl.img=imgtk
如果stop==True:
vc.release()
中断#停止循环,从而停止更新标签和读取imagge帧
cv2.等待键(1)
vc.release()
def view_frame_video2():
vc=cv2.VideoCapture('C:\\Users\\Devendra\\Desktop\\VID_20190129_162657.mp4')
如果vc.isopend():
rval,frame=vc.read()
其他:
rval=False
而rval:
rval,frame=vc.read()
img=Image.fromarray(帧)
img=调整大小(img)
imgtk=ImageTk.照片图像(img)
lbl.config(image=imgtk)
lbl.img=imgtk
如果stop==True:
vc.release()
中断#停止循环,从而停止更新标签和读取imagge帧
cv2.等待键(1)
vc.release()
def停止()
全局停止
停止=正确
def play():
停止=错误
t=线程。线程(目标=视图\帧\视频)
t、 开始()
def play2():
停止=错误
t=线程。线程(目标=视图\帧\视频2)
t、 开始()
win=Tk()
win.title(“数据记录器”)
win.geometry('1300x650')
停止=无
screenWH=(win.winfo_屏幕宽度(),win.winfo_屏幕高度())
siz=(600600)
标签(text='假手测试软件',font=('times',22),fg='Red',width=104,height=2).pack()
def导入_csv_数据():
全球v
csv_file_path=askopenfilename()
打印(csv文件路径)
v、 设置(csv文件路径)
标签(win,text='File Path',font=('arial',10),fg='black',width=104,height=2).pack()
v=StringVar()
entry=entry(textvariable=v)
entry.pack()
按钮(text='Browse Data Set',command=import\u csv\u Data).pack()
frm=帧(背景=黑色)
frm_.pack()
lbl=标签(frm_3;,bg='黑色')
lbl.pack(expand=True)
lbl.bind(“”,尺寸)
按钮框架=框架(win)
按钮框架包装(侧面=底部)
按钮(text='Test 1',command=play,fg=“Green”,高度=2,宽度=20)。包装(侧面=左侧)
按钮(text='Test 2',command=play2,fg='Green',高度=2,宽度=20)。包装(侧面=左侧)#,padx=21,pady=18)
按钮(buttonframe,text='Stop',command=Stop,fg='Orange',高度=2,宽度=20)。包装(侧面=左侧,padx=20,pady=18)
按钮(按钮框,text='Exit',fg=“Red”,命令=win.destroy,高度=2,宽度=20)。包装(侧面=左侧,padx=20,pady=18)
win.mainloop()
当我在测试1之后单击测试2时,应该关闭测试1并播放测试2
当我停止播放测试1,2并再次单击任何按钮时,应该重新开始播放测试1,2。主要问题是,每当您单击
test1
test2
按钮时,您的代码将创建新线程。但是,旧线程仍在运行,因此有多个线程更新
lbl
,从而导致图像重叠。为了克服这个问题,创建了一个用于播放视频的线程。
test1
test2
按钮用于选择哪个视频文件作为源。以下是基于您的代码修改(和简化)的代码:

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()

您需要在
play()
play2()
函数中将
stop
声明为全局。谢谢您,它成功了:)再来一个。。当我播放测试1并在测试2运行时单击测试2,测试1不停止,测试2播放任何IDEA时,这是因为测试1的线程仍在运行,因为启动测试2线程时,
stop
的值仍然为False。您需要找到停止测试1线程的方法。感谢您的帮助。。你能告诉我一件事吗,如果我能把一张图片而不是那张黑框。只要把
image.new(…)
改成
image.open('/path/to/image')