我不是WATSUP用户,但我使用pywinauto做了一些非常类似的事情——在我的例子中,我运行了许多自动测试,这些测试打开了各种第三方程序,以类似的方式,抛出了不方便的警告对话框。处理您不知道的对话框有点困难,但是如果您知道哪些对话框出现,但不知道它们何时出现,您可以启动一个线程来处理这些弹出窗口。下面是我正在做的一个简单的例子,它使用了pywinauto,但您可以将该方法应用于WATSUP:import time
import threading
class ClearPopupThread(threading.Thread):
def __init__(self, window_name, button_name, quit_event):
threading.Thread.__init__(self)
self.quit_event = quit_event
self.window_name = window_name
self.button_name = button_name
def run(self):
from pywinauto import application, findwindows
while True:
try:
handles = findwindows.find_windows(title=self.window_name)
except findwindows.WindowNotFoundError:
pass #Just do nothing if the pop-up dialog was not found
else: #The window was found, so click the button
for hwnd in handles:
app = application.Application()
app.Connect(handle=hwnd)
popup = app[self.window_name]
button = getattr(popup, self.button_name)
button.Click()
if self.quit_event.is_set():
break
time.sleep(1) #should help reduce cpu load a little for this thread
本质上,这个线程只是一个无限循环,它按名称查找弹出窗口,如果找到它,它单击一个按钮关闭窗口。如果你有很多弹出窗口,你可以在每个弹出窗口中打开一个线程(不过,这不是非常高效的bug)。因为这是一个无限循环,所以我让线程查看是否设置了事件,以允许我从主程序停止线程。所以,在主程序中,我做如下操作:
^{pr2}$
这不一定是解决你问题的唯一方法,但对我来说是有效的。抱歉,在处理WATSUP时我真的帮不了你多少忙(我总是发现pywinauto更容易使用),但是我注意到在WATSUP主页(http://www.tizmoi.net/watsup/intro.html)上,示例2在不使用线程的情况下也做了类似的事情,即查找一个命名窗口并单击该窗口上的特定按钮。在