如何使自毁 tkinter window?

How to make self destroying tkinter window?

我想在 tkinter window 中播放 mp4 视频几秒钟,但要关闭 windows 我需要单击关闭按钮。我想,播放视频后它会自动关闭 windows。

这是代码:

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

上面的代码播放一个mp4视频,我希望播放完这个视频后它会自动关闭而不用点击关闭按钮。

您可以在该循环之后添加 root.destroy() 以关闭 window。

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