import os
import sys
import pyautogui
import time
import xlrd
import pyperclip
#定义鼠标事件
#pyautogui库其他用法 https://blog.csdn.net/qingfengxd1/article/details/108270159
def mouseClick(clickTimes,lOrR,img,reTry):
if reTry == 1:
while True:
location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
if location is not None:
pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
break
print("未找到匹配图片,0.1秒后重试")
time.sleep(1)
elif reTry == -1:
while True:
location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
if location is not None:
pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
time.sleep(1)
elif reTry > 1:
i = 1
while i < reTry + 1:
location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
if location is not None:
pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
print("重复")
i += 1
time.sleep(1)
#单击左键1次
def l1(img,reTry=1):
mouseClick(1, "left", img, reTry)
print("单击左键", img)
#单击左键2次
def l2(img,reTry=1):
mouseClick(2, "left", img, reTry)
print("双击左键", img)
#单击右键1次
def r1(img,reTry=1):
mouseClick(1, "left", img, reTry)
print("右键", img)
#键入contents内容
def cp(contents):
inputValue = contents
pyperclip.copy(inputValue)
pyautogui.hotkey('ctrl', 'v')
print("输入:",contents)
#滚轮scroll距离
def g(scroll):
pyautogui.scroll(int(scroll))
print("滚轮滑动", int(scroll), "距离")
#等待waitTime多久
def w(waitTime):
time.sleep(waitTime)
print("等待", waitTime, "秒")
def full_name(img):
pwd=os.getcwd()
img_dir=os.path.join(pwd,"imgs")
if not os.path.exists(img_dir):
print("img_path not exist,check it right now!")
sys.exit()
return os.path.join(img_dir,img)
#任务
def mainWork():
l1(full_name("1.jpg"))
l1(full_name("4.png"))
l1(full_name("5.png"))
l1(full_name("3.png"))
cp("飞飞臭宝我爱你,想要和你飞到天上去,嘤嘤嘤!")
l1(full_name("2.jpg"))
if __name__ == '__main__':
key = pyautogui.confirm(text='1.循环一次 2.无限循环', title='选择功能', buttons=range(1,3))
if key=='1':
#循环拿出每一行指令
mainWork()
elif key=='2':
while True:
mainWork()
time.sleep(0.1)
print("等待0.1秒")
在同级目录下新建imgs文件夹,把所有事件放进那个文件夹,然后在mainWork函数中编辑对应的操作,即可运行。