pyautogui

一、准备

文档地址:官方文档
介绍:The purpose of PyAutoGUI is to provide a cross-platform Python module for GUI automation for human beings. The API is designed to be as simple as possible with sensible defaults.

二、示例


>>> import pyautogui
>>> screenWidth, screenHeight = pyautogui.size() #获取屏幕宽高
>>> currentMouseX, currentMouseY = pyautogui.position() #获取鼠标x、y坐标值
>>> pyautogui.moveTo(100, 150) #将鼠标移动至该坐标
>>> pyautogui.click() #模拟鼠标点击左键
>>> pyautogui.moveRel(None, 10)  # 表示在y轴下移10像素
>>> pyautogui.doubleClick() # 双击鼠标左键
>>> pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad)  # 在两秒内移动至目标位置
>>> pyautogui.typewrite('Hello world!', interval=0.25)  # 每次输入间隔0.25s
>>> pyautogui.press('esc') # 按下esc键
>>> pyautogui.keyDown('shift') # 按下shift键,并且不放开
>>> pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])# 在按住shift的情况,按其他的键
>>> pyautogui.keyUp('shift') # 松开shift键
>>> pyautogui.hotkey('ctrl', 'c') # 直接使用热键

尝试:画图
打开画图工具

>>> distance = 200
>>> while distance > 0:
        pyautogui.dragRel(distance, 0, duration=0.5)   # move right
        distance -= 5
        pyautogui.dragRel(0, distance, duration=0.5)   # move down
        pyautogui.dragRel(-distance, 0, duration=0.5)  # move left
        distance -= 5
        pyautogui.dragRel(0, -distance, duration=0.5)  # move up

安全设置
如果运行时出现错误,或不可控时,设置pyautogui.FAILSAFE=True,将会抛出错误pyautotui.FailSageException,然后将鼠标移动至屏幕左上角,可以停止程序

import pyautogui
pyaugotui.FAILSAFE = True

操作间隔

import pyautogui
pyaugotui.PAUSE = 2.5
pyautogui.moveTo(100, 100)
pyautogui.click() 
# 两步之间运行时间间隔为2.5s

常用函数

pyautogui.position()
pyautogui.size()
pyaugotui.onScreen()
方法 描述
pyautogui.position() 获取当前鼠标坐标值
pyautogui.size() 获取当前屏幕大小
puautogui.onScreen(x,y) 判断坐标是否在屏幕上
pyautogui.moveTo(x,y,duration=seconds) 指定时间内移动至x,y处
pyautogui.moveRel(xOffset, yOffset, duration=seconds) 在指定时间内,以当前鼠标为起点,移动xOffset,yOffset
pyautogui.dragTo(x,y,duration=num_seconds)
pyautogui.dragRel(xOffset, yOffset, duration=num_seconds)

未完待续

你可能感兴趣的:(python库)