一段时间后关闭 tkinter 窗口 [英] Close a tkinter window after a period of time

查看:46
本文介绍了一段时间后关闭 tkinter 窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段 Python 代码,它应该在一段时间内打开一个新窗口,然后关闭该窗口.该窗口是通过单击按钮触发的.这是我所拥有的基础知识.

I have a piece of Python code that is supposed to open a new window for a period of time and then close the window. The window is triggered by clicking a button. Here is the basics of what I have.

def restore(self):
    self.restore = Toplevel()

    message = "Select an available Backup to Restore to."

    Label(self.restore, text=message).pack()
    # We then create and entry widget, pack it and then
    # create two more button widgets as children to the frame.

    os.chdir('.')
    for name in os.listdir("."): 
        if os.path.isdir(name):
            self.button = Button(self.restore, text=name,command=self.restoreCallBack)
            self.button.pack(side=BOTTOM,padx=10)

def restoreCallBack(self):
    self.restoreCB = Toplevel()

    message = "Please wait while the database is restored..."
    Label(self.restoreCB, text=message, padx=100, pady=20).pack()

    time.sleep(5)

    self.restore.destroy()
    self.restoreCB.destroy()

我需要将 restoreCallBack 窗口显示 5 秒钟,然后关闭窗口.谢谢!

I need the restoreCallBack window to be displayed for 5 seconds, then the windows to close. Thanks!

推荐答案

看看 after 方法.例如:

widget.after(5000,callback)

您不应该在 GUI(的主线程)中使用 sleep -- 整个事情都会冻结.

You shouldn't use sleep in (the main thread of) a GUI -- The entire thing will just freeze.

这篇关于一段时间后关闭 tkinter 窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆