实现自动化控制,需求:在电脑屏幕中两个点出现红色,然后用鼠标点击右下方刷新,实现代码如下,可是在实际运行中,运行一段时间就会失效(假死)不结束也不报错。请各位大佬帮忙看看能否有解决方案,谢谢!
from ctypes import windll
import win32api
import win32con
import time
from datetime import datetime
def get_color1(x, y): #获取屏幕固定1像素rgb值
list1 =[ ]
gdi32 = windll.gdi32
user32 = windll.user32
hdc = user32.GetDC(None) # 获取颜色值
pixel = gdi32.GetPixel(hdc, x, y) # 提取RGB值
r = pixel & 0x0000ff
g = (pixel & 0x00ff00) >> 8
b = pixel >> 16
return [r,g,b]
def mouse_buten(m,n):#鼠标点击
width = windll.user32.GetSystemMetrics(0) # 获取屏幕宽度
height = windll.user32.GetSystemMetrics(1) # 高度
# print(width, height)
windll.user32.SetCursorPos(m, n)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, m, n) # 鼠标左键按下
time.sleep(0.05)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, m, n) #鼠标左键抬起0
time.sleep(1)
h=0
var = 0
while var == 0:
list1 = get_color1(1290, 57)
list2 = get_color2(1250, 88)
if (50 < list1[0] < 61 and 170 < list1[1] < 180 and 74 < list1[2] < 106) or \
(60 < list2[0] < 61 and 60 < list2[1] < 65 and 64 < list2[2] < 66):
mouse_buten(1220,757)
now_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
h += 1
print(h, "刷新成功", now_time)
time.sleep(3)
else:
time.sleep(0.5)