在 Tkinter 中在一段时间后自动关闭窗口
为了关闭一个 Tkinter 应用程序,我们通常使用 destroy() 方法关闭父窗口。要在一个特定的时间限制之后自动关闭 Tkinter 窗口,我们必须使用 after(以毫秒表示的时间,回调) 方法,方法是指定时间和在一段时间限制后需要运行的回调函数。
示例
#Import the required Libraries
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter frame
win = Tk()
#Set the geometry of tkinter frame
win.geometry("750x270")
#Initialize a Label widget
Label(win, text= "This window will get closed after 3 seconds...",
font=('Helvetica 20 bold')).pack(pady=20)
#Automatically close the window after 3 seconds
win.after(3000,lambda:win.destroy())
win.mainloop()输出
运行以上代码将显示一个窗口,该窗口将在 3 秒后关闭。

给定的输出窗口将在 3 秒后关闭,可以通过更改 after (time, callback) 方法中的值来修改。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP