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

标签 python tkinter

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

最佳答案

您可以使用 after 函数在延迟结束后调用函数,并使用 destroy 关闭窗口。

举个例子

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()

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

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 - 如何启动自毁的 Python/Tkinter 对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1917198/

相关文章:

python - Tkinter 销毁顶层窗口也会销毁其他窗口

python - Django httpresponse 剥离 CR

python - 将 Pelican 文章拆分为多个页面

python - for 循环打印括号内

python - 对数据框中的一行条件进行外部处理

python - 使用python从类中返回变量

python - 无法使用OpenCv/Tkinter/Python保存和显示图像

python - Tkinter 多项操作

python - 从 dict 生成 django 2.0 模型

python - tkinter 按钮边框的一半是白色的?