如何启动自毁的 Python/Tkinter 对话框?

编程入门 行业动态 更新时间:2024-10-21 19:01:45
本文介绍了如何启动自毁的 Python/Tkinter 对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

好的,我想组装一个 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 06:56:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390624.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对话框   Python   Tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!