如何检查线程是否已在Python中结束

时间:2016-09-27 13:50:35

标签: python-3.x user-interface tkinter

我正在开发我的第一个应用程序,并且已经关注如何使用tkinter和python设置工作GUI的一些教程。我的应用程序首先播放启动画面动画,一旦完成,切换到" UserSetup"帧。我仍然忙于尝试重新编码播放此视频的功能,以便我可以将其作为一种方法(或任何有效的方法)。我想知道的是,如果有一种方法可以检查播放此视频的线程何时停止?现在播放的任何其他动画同时运行,我不想要那样。我基本上希望它只能继续使用下一组动画或者任何一旦启动画面动画停止。

这是相当多的代码......

# ======================
# imports
# ======================
import tkinter as tk
import imageio
import threading
from PIL import Image, ImageTk

LARGE_FONT = ("Segoe UI", 18)


# =======================
# INITIALISATION - MAIN
# =======================
class MyApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side='top', fill='both', expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (SplashScreen, UserSetup, TimerSetup):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky='nsew')
        self.show_frame(SplashScreen)


    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


# ======================
# PAGES
# ======================
""" To add new pages, just create a page class which inherits from tk.Frame,
    add it to the dictionary, and add buttons for navigation """


class SplashScreen(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        video_name = "img//splash.avi"  # This is your video file path
        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
            controller.show_frame(UserSetup)

        def forget():
            my_label.pack_forget()

        my_label = tk.Label(self)
        my_label.pack()
        my_label.after(ms=15000, func=forget)
        thread = threading.Thread(target=stream, args=(my_label,))
        thread.daemon = 1
        thread.setName("splash")
        thread.start()
        print(thread.getName())


class UserSetup(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)


class TimerSetup(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)


# ======================
# INITIALISATION (MAIN)
# ======================
app = MyApp()
#app.overrideredirect(True)
app.mainloop()

1 个答案:

答案 0 :(得分:0)

我过去曾使用is_alive() ..形式为:

while thread_name.is_alive():
    pass

这将等待线程完成,然后继续执行脚本的下一部分。

希望这有帮助! 路加