如何启动自毁的 Python/Tkinter 对话框? [英] How to launch a Python/Tkinter dialog box that self-destructs?

查看:30
本文介绍了如何启动自毁的 Python/Tkinter 对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我想组装一个 Python/Tkinter 对话框,显示一条简单的消息并在 N 秒后自毁.有没有简单的方法可以做到这一点?

Ok, I would like to put together a Python/Tkinter dialog box that displays a simple message and self-destructs after N seconds. Is there a simple way to do this?

推荐答案

您可以使用 after 函数在延迟过去后调用函数并使用 destroy 关闭窗户.

You can use the after function to call a function after a delay elapsed and the destroy to close the window.

这是一个例子

from Tkinter import Label, Tk
root = Tk()
prompt = 'hello'
label1 = Label(root, text=prompt, width=len(prompt))
label1.pack()

def close_after_2s():
    root.destroy()

root.after(2000, close_after_2s)
root.mainloop()

更新:后面的文档字符串说:

Update: The after docstring says:

在给定时间后调用一次函数.MS 以毫秒为单位指定时间.FUNC 给出应调用的函数.附加参数作为函数调用的参数给出.返回标识符以使用 after_cancel 取消调度.

Call function once after given time. MS specifies the time in milliseconds. FUNC gives the function which shall be called. Additional parameters are given as parameters to the function call. Return identifier to cancel scheduling with after_cancel.

这篇关于如何启动自毁的 Python/Tkinter 对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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