I am developing a GUI application using Tkinter, where the user can click a button to start playing a video. I am using MoviePy to ensure that the video and audio play synchronously, but the issue I am facing is that the video opens in a separate window. This window does not close after the video ends, which prevents the user from interacting with the main GUI.
Here’s a simplified version of my code:
import tkinter as tk
from moviepy.editor import VideoFileClip
root = tk.Tk()
root.title()
root.attributes('-fullscreen', True)
# Function to play the video
def play_video():
clip = VideoFileClip(video_path) # Replace with your video path
clip.preview()
play_button = tk.Button(root, text="Play", command=play_video)
play_button.pack(padx=50, pady=50)
root.mainloop()
Current Issue:
Goal:
I want to ensure that after the video is played, users can continue working in the GUI. This might be achieved in one of the following ways:
Background:
I appreciate any help or suggestions!
The preview()
function does not include parameters to change this behavior.
However, MoviePy uses PyGame internally, so it is possible to close the window once a video has finished using pygame.quit()
:
import tkinter as tk
import pygame
from moviepy.editor import VideoFileClip
root = tk.Tk()
root.title()
root.attributes('-fullscreen', True)
# Function to play the video
def play_video():
clip = VideoFileClip(video_path) # Replace with your video path
clip.preview()
pygame.quit()
play_button = tk.Button(root, text="Play", command=play_video)
play_button.pack(padx=50, pady=50)
root.mainloop()