2016-04-06 166 views
1

我已經知道如何用Python來做出一個消息:如何使Python在幾秒內自動關閉消息框?

import ctypes 

ctypes.windll.user32.MessageBoxW(0, 'test', "Reminding", 0) 

不過,我希望它本身在幾秒鐘就會出現後關閉。

有什麼方法可以實現嗎?

的僞代碼:

def AutoCloseMessageBoxW(text, title, close_until_seconds) 

我已經找到了很多方法來實現這一由其他語言如C#和Java。

但我只是找不到任何方法來實現它的Python。

+0

您通過user32調用的標準Windows消息框沒有任何內置自閉機制。如果C#和Java允許這樣做,那是因爲他們創建了自己的工作方式不同的消息框。 –

回答

-1
import ctypes 
import threading 
import time 
#ctypes.windll.user32.MessageBoxA(0, 'test', "Reminding", 0) 

def worker(title,close_until_seconds): 
    time.sleep(close_until_seconds) 
    wd=ctypes.windll.user32.FindWindowA(0,title) 
    ctypes.windll.user32.SendMessageA(wd,0x0010,0,0) 
    return 

def AutoCloseMessageBoxW(text, title, close_until_seconds): 
    t = threading.Thread(target=worker,args=(title,close_until_seconds)) 
    t.start() 
    ctypes.windll.user32.MessageBoxA(0, text, title, 0) 


AutoCloseMessageBoxW('112','TEST_CLOSE',3) 
+0

代碼轉儲雖然可能提供解決方案,但對於試圖向他們學習的人而言可能並不清楚。你能用幾句話來描述你的方法的關鍵要素以及它如何/爲什麼起作用? – Reti43

+0

由於標題不能保證是唯一的,所以擁有一個按標題殺死窗口的線程是非常危險的。特別是如果你在用戶解散第一個消息後得到第二個消息框。 –