Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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 tkinter的视频播放器,当我暂停视频时,我无法重新播放_Python_Video_Tkinter_Pause - Fatal编程技术网

Python tkinter的视频播放器,当我暂停视频时,我无法重新播放

Python tkinter的视频播放器,当我暂停视频时,我无法重新播放,python,video,tkinter,pause,Python,Video,Tkinter,Pause,我正在创建GUI来播放视频文件。问题是,当我暂停视频时,“播放”按钮无法重新播放该视频,我必须再次选择“视频文件” 注意:因为我想在同一tkinter窗口中显示视频,所以我不使用OpenCV imshow命令。相反,我使用的是“window.after”方法 以下是我的代码: 我尝试使用“self.pause”变量来控制暂停状态。单击“暂停”按钮时,此布尔变量变为True。然而,当我再次单击“播放”按钮时,我找不到合适的位置将其设置为错误 from tkinter import * from t

我正在创建GUI来播放视频文件。问题是,当我暂停视频时,“播放”按钮无法重新播放该视频,我必须再次选择“视频文件”

注意:因为我想在同一tkinter窗口中显示视频,所以我不使用OpenCV imshow命令。相反,我使用的是“window.after”方法

以下是我的代码:

我尝试使用“self.pause”变量来控制暂停状态。单击“暂停”按钮时,此布尔变量变为True。然而,当我再次单击“播放”按钮时,我找不到合适的位置将其设置为错误

from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
import PIL.Image, PIL.ImageTk
import cv2


class videoGUI:

    def __init__(self, window, window_title):

        self.window = window
        self.window.title(window_title)

        top_frame = Frame(self.window)
        top_frame.pack(side=TOP, pady=5)

        bottom_frame = Frame(self.window)
        bottom_frame.pack(side=BOTTOM, pady=5)

        self.pause = False   # Parameter that controls pause button

        self.canvas = Canvas(top_frame)
        self.canvas.pack()

        # Select Button
        self.btn_select=Button(bottom_frame, text="Select video file", width=15, command=self.open_file)
        self.btn_select.grid(row=0, column=0)

        # Play Button
        self.btn_play=Button(bottom_frame, text="Play", width=15, command=self.play_video)
        self.btn_play.grid(row=0, column=1)

        # Pause Button
        self.btn_pause=Button(bottom_frame, text="Pause", width=15, command=self.pause_video)
        self.btn_pause.grid(row=0, column=2)

        self.delay = 15   # ms

        self.window.mainloop()


    def open_file(self):

        self.pause = False

        self.filename = filedialog.askopenfilename(title="Select file", filetypes=(("MP4 files", "*.mp4"),
                                                                                         ("WMV files", "*.wmv"), ("AVI files", "*.avi")))
        print(self.filename)

        # Open the video file
        self.cap = cv2.VideoCapture(self.filename)

        self.width = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        self.height = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

        self.canvas.config(width = self.width, height = self.height)


    def get_frame(self):   # get only one frame

        try:

            if self.cap.isOpened():
                ret, frame = self.cap.read()
                return (ret, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

        except:
            messagebox.showerror(title='Video file not found', message='Please select a video file.')


    def play_video(self):

        # Get a frame from the video source, and go to the next frame automatically
        ret, frame = self.get_frame()

        if ret:
            self.photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(frame))
            self.canvas.create_image(0, 0, image = self.photo, anchor = NW)

        if not self.pause:
            self.window.after(self.delay, self.play_video)


    def pause_video(self):
        self.pause = True


    # Release the video source when the object is destroyed
    def __del__(self):
        if self.cap.isOpened():
            self.cap.release()

##### End Class #####


# Create a window and pass it to videoGUI Class
videoGUI(Tk(), "EnJapan")
如果我在“播放视频”功能中编写以下代码:

self.pause = False

暂停按钮将不起作用。因为“window.after”方法会自动调用“play_video”函数,并将“self.pause”设置为False。因此,暂停按钮将不起作用

我将为播放按钮回调创建另一个方法。大概是这样的:

def play_start(self):
    self.pause = False
    self.play_video()
但是,如果播放按钮已经在播放,我会确保您禁用播放按钮。否则,如果多次按下播放按钮,您可能会有多个播放视频的“实例”


另一种方法是将“播放”和“暂停”按钮组合在一起,这样它就可以切换
self.pause
的值。然后你可以有一个按钮和一个回调函数

问题:暂停按钮将无效

参考

  • 注册在给定时间后调用的回调

  • 取消回调


要取消
self.play\u video
的allready排队
事件,请更改以下内容:

def play_video(self):
    ...

    if self.pause:
        self.window.after_cancel(self.after_id)
    else:
        self.after_id = self.window.after(self.delay, self.play_video)

只需添加play_video(self)方法:


使用
self.after\u id=self.window.after(…
self.after\u id)
你能给我更多提示我应该在代码中的什么地方使用它吗?@stovfi“self.after\u id”代表什么?@Ekin“self.after\u id
代表什么?”:我已使用参考链接更新了我的答案。
after\u id
列出了从
返回的标识符。
在(…
之后。
if self.pause == True:
    self.pause = False
    return
 
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
import PIL.Image, PIL.ImageTk
import cv2


class videoGUI:

    def __init__(self, window, window_title):

        self.window = window
        self.window.title(window_title)

        top_frame = Frame(self.window)
        top_frame.pack(side=TOP, pady=5)

        bottom_frame = Frame(self.window)
        bottom_frame.pack(side=BOTTOM, pady=5)

        self.pause = False   # Parameter that controls pause button

        self.canvas = Canvas(top_frame)
        self.canvas.pack()

        # Select Button
        self.btn_select=Button(bottom_frame, text="Select video file", width=15, command=self.open_file)
        self.btn_select.grid(row=0, column=0)

        # Play Button
        self.btn_play=Button(bottom_frame, text="Play", width=15, command=self.play_video)
        self.btn_play.grid(row=0, column=1)

        # Pause Button
        self.btn_pause=Button(bottom_frame, text="Pause", width=15, command=self.pause_video)
        self.btn_pause.grid(row=0, column=2)

        # Resume Button
        self.btn_resume=Button(bottom_frame, text="resume", width=15, command=self.resume_video)
        self.btn_resume.grid(row=0, column=3)

        self.delay = 15   # ms

        self.window.mainloop()


    def open_file(self):

        self.pause = False

        self.filename = filedialog.askopenfilename(title="Select file", filetypes=(("MP4 files", "*.mp4"),
                                                                                         ("WMV files", "*.wmv"), ("AVI files", "*.avi")))
        print(self.filename)

        # Open the video file
        self.cap = cv2.VideoCapture(self.filename)

        self.width = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        self.height = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

        self.canvas.config(width = self.width, height = self.height)


    def get_frame(self):   # get only one frame

        try:

            if self.cap.isOpened():
                ret, frame = self.cap.read()
                return (ret, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

        except:
            messagebox.showerror(title='Video file not found', message='Please select a video file.')


    def play_video(self):

        # Get a frame from the video source, and go to the next frame automatically
        ret, frame = self.get_frame()

        if ret:
            self.photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(frame))
            self.canvas.create_image(0, 0, image = self.photo, anchor = NW)

        if not self.pause:
            self.window.after(self.delay, self.play_video)


    def pause_video(self):
        self.pause = True

#Addition
    def resume_video(self):
        self.pause=False
        self.play_video()


    # Release the video source when the object is destroyed
    def __del__(self):
        if self.cap.isOpened():
            self.cap.release()

##### End Class #####


# Create a window and pass it to videoGUI Class
videoGUI(Tk(), "EnJapan")