一定时间后自动关闭窗口 [英] Automatically close window after a certain time

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

问题描述

在一个类中,在一个函数中我正在创建一个 Tkinter 画布.此函数正在被另一个类调用,我希望 Tkinter 窗口弹出 30 秒然后自行关闭.我叫它

In a class, in a function I am creating a Tkinter Canvas. This function is being called by another class, I would like for the Tkinter window to pop up for 30 seconds and then close itself. I have it call

master.mainloop()
time.sleep(30)
master.destroy() 

但是我得到一个错误

"elf.tk.call('destroy', self._w)_tkinter.TclError:无法调用销毁"命令:应用程序已被销毁"

"elf.tk.call('destroy', self._w) _tkinter.TclError: can't invoke "destroy" command: application has been destroyed"

那么我怎样才能让它自己关闭呢?

So how can I have it close itself?

推荐答案

不要将 time.sleep() 与 tkinter 一起使用.相反,调用函数 after 在要关闭的小部件上.

Don't use time.sleep() with tkinter. Instead, call the function after on the widget you want to close.

这是最简单的例子:

import tkinter as tk

w = tk.Tk()
w.after(30000, lambda: w.destroy()) # Destroy the widget after 30 seconds
w.mainloop()

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

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