【问题标题】:Close a tkinter window after a period of time一段时间后关闭 tkinter 窗口
【发布时间】:2012-06-16 19:50:00
【问题描述】:

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

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 秒钟,然后关闭窗口。谢谢!

【问题讨论】:

    标签: python time tkinter window


    【解决方案1】:

    看看after 方法。例如:

    widget.after(5000,callback)
    

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

    【讨论】:

      猜你喜欢
      • 2017-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      相关资源
      最近更新 更多