![](/img/trans.png)
[英]Open tkinter messagebox that self-destructs in the lower right corner of the screen instead of the center
[英]How to launch a Python/Tkinter dialog box that self-destructs?
好吧,我想把一個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()
更新:后文檔字符串說:
在給定時間后調用一次函數。 MS以毫秒為單位指定時間。 FUNC給出了應該調用的函數。 附加參數作為函數調用的參數給出。 返回使用after_cancel取消調度的標識符。
你也可以使用一個線程。
此示例使用Timer在指定的時間后調用destroy()。
import threading
import Tkinter
root = Tkinter.Tk()
Tkinter.Frame(root, width=250, height=100).pack()
Tkinter.Label(root, text='Hello').place(x=10, y=10)
threading.Timer(3.0, root.destroy).start()
root.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.