How do I create a simple popup video in Python and close it?

183 Views Asked by At

I have a simple pokemon-battle type project I'm doing for a college but I want to add more visuals to it by adding small 2 sec animations that plays everytime a button is clicked. I thought about using a toplevel window which plays a video using tkvideo. It works, but I can't figure out how to close it after 2 secs OR after it loops the video once.

Can anyone offer suggestions to a newbie? P.S. I used Toplevel because there is already a main root window where the player can click buttons.

from tkinter import *
from tkvideo import tkvideo

window = tkinter.Toplevel()
window.title("Video PLayer")

lblVideo = Label(window)
lblVideo.pack()

player = tkvideo("Hero_Slash_Attack_Animation.mp4", lblVideo, loop=1, size=(500,500),)

player.play()

window.mainloop()

The code up above is what I used to play a secondary video window but. But I can't figure out how to close that specific top level. I tried w.destroy() but it didn't remove it. Instead, the video just keeps looping and staying.

2

There are 2 best solutions below

4
On

You can use tkVideoPlayer instead of tkvideo. Here is a link to the documentation.

For example:
import tkinter as tk
from tkVideoPlayer import TkinterVideo

root = tk.Tk()

def close_video(win):
    if win.winfo_exists():
        win.destroy()

def create_window():
    window = tk.Toplevel()
    window.lift()

    player = TkinterVideo(master=window, scaled=True)
    player.load(r"video.mp4")
    player.pack(expand=True, fill="both")

    window.after(2000, close_video, window) # close win after 2 sec
    player.bind("<<Ended>>", lambda event: close_video(window)) # close the win if video ended

    player.play() # play the video

tk.Button(root, text="Show Video", command=create_window).pack()
root.mainloop()
  • Created a function for closing the window.
  • Bound the event of video ended to that function
  • Scheduled the window to close 2 seconds after the window is launched.
0
On

You can simulate the virtual event feature of tkvideoplayer module mentioned in other answer using tkvideo module by creating a custom class inherited from tkvideo and overload the class method load():

import tkinter as tk
from tkvideo import tkvideo as orig_tkvideo

# custom class inherits from tkvideo
class tkvideo(orig_tkvideo):
    # overload load() method
    def load(self, *args):
        try:
            super().load(*args)
            # vidoe ended, generate virtual event "<<End>>" to container
            self.label.event_generate("<<End>>")
        except Exception as ex:
            print(ex)  # or pass

window = tk.Toplevel()
window.title("Video PLayer")
window.transient(window.master)

lblVideo = tk.Label(window)
lblVideo.pack()
# close window upon receiving virtual event "<<End>>"
lblVideo.bind("<<End>>", lambda e: window.destroy())

player = tkvideo("_output.mp4", lblVideo, loop=0, size=(500, 500))
player.play()

# close window after 2 seconds
lblVideo.after(2000, window.destroy)

window.mainloop()