Home > front end >  How to make self destroying tkinter window?
How to make self destroying tkinter window?

Time:05-18

I want to play a mp4 video for a few seconds in tkinter window, but for closing windows i need to click in close button. i want, after playing the video it will self close the windows.

This is the code:

import tkinter as tk, threading
import imageio
from PIL import Image, ImageTk

video_name = "C:\\Users\\Admin\\Desktop\\CA SOFTWARES\\CA\\boot.mp4" 
video = imageio.get_reader(video_name)

def stream(label):

    for image in video.iter_data():
        frame_image = ImageTk.PhotoImage(Image.fromarray(image))
        label.config(image=frame_image)
        label.image = frame_image

if __name__ == "__main__":

    root = tk.Tk()
    my_label = tk.Label(root)
    my_label.pack()
    thread = threading.Thread(target=stream, args=(my_label,))
    thread.daemon = 1
    thread.start()
    root.mainloop()

The above code to play a mp4 video, i want after playing this video it will close automatically without clicking on close button.

CodePudding user response:

You can add root.destroy() after that loop to close the window.

def stream(label):
    for image in video.iter_data():
        # ...
    root.destroy()
  • Related