2016-04-06 165 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

由于标题不能保证是唯一的,所以拥有一个按标题杀死窗口的线程是非常危险的。特别是如果你在用户解散第一个消息后得到第二个消息框。 –