Python tkinter 的视频播放器,当我暂停视频时,我无法重新播放 [英] Video player by Python tkinter, When I pause video, I cannot re-play

查看:43
本文介绍了Python tkinter 的视频播放器,当我暂停视频时,我无法重新播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating GUI to play video files. The problem is that when I pause video, Play button cannot re-play that video, and I have to select video file again.

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

Note: Since I want to show video in the same tkinter window, I don't use OpenCV imshow command. Instead, I am using "window.after" method.

以下是我的代码:

我尝试使用self.pause"变量来控制暂停状态.当我单击暂停按钮时,这个布尔变量变为 True.但是,当我再次单击播放"按钮时,我找不到合适的位置将其设为 False.

I try to use "self.pause" variable to control pause status. When I click Pause button, this Boolean variable becomes True. However, I couldn't find suitable place to make it False when I click Play button again.

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

如果我在play_video"函数中编写以下代码:

If I write following code in "play_video" function:

self.pause = False

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

Pause button will not work. Because "window.after" method calls "play_video" function automatically and make the "self.pause" as False. Therefore, Pause button will have no effect.

推荐答案

问题:暂停按钮无效.

参考:

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

Registers an callback that is called after a given time.

  • Tkinter.Widget.after_cancel-method - after_cancel(id)

    取消回调.

  • 取消 self.play_video 的所有已排队events,请更改以下内容:

    To cancel the allready queued events for self.play_video change the following:

    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)
    

    这篇关于Python tkinter 的视频播放器,当我暂停视频时,我无法重新播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆