autogui的使用

pyautoui简介

PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作,多平台支持(Windows,OS X,Linux)。

安装扩展包

pip install pyautogui

操作样例

import pyautogui
import time
screenWidth, screenHeight = pyautogui.size() # 屏幕尺寸
print("screenWidth:",screenWidth)
print("screenHeight:",screenHeight)

while True:
    mouseX, mouseY = pyautogui.position() # 返回当前鼠标位置,注意坐标系统中左上方是(0, 0)
    print("mouseX:",mouseX,"mouseY:",mouseY)
    time.sleep(1)
    break

pyautogui.PAUSE = 1.5 # 每个函数执行后停顿1.5秒
pyautogui.FAILSAFE = True # 鼠标移到左上角会触发FailSafeException,因此快速移动鼠标到左上角也可以停止

print("moveTo center")
pyautogui.moveTo(screenWidth/2, screenHeight/2) # 基本移动

time.sleep(1)
print("moveTo 100 200 in 2")
pyautogui.moveTo(100, 200, duration=2) # 移动过程持续2s完成

time.sleep(1)
print("moveTo None 500 in 2")
# pyautogui.moveTo(None, 500) # X方向不变,Y方向移动到500

time.sleep(1)
print("moveTo -40 500 in 2")
pyautogui.moveRel(-40, 500) # 相对位置移动

pyautogui.click(1279, 374)
pyautogui.typewrite('hello world!')

# 点击+向下拖动
pyautogui.click(941, 34, button='left')
pyautogui.dragRel(0, 100, button='left', duration=2)

pyautogui.click(300, 400, button='right') # 包含了move的点击,右键
pyautogui.click(clicks=2, interval=0.25) # 双击,间隔0.25s
hello world!
pyautogui.scroll(-10)

pyautogui.click(screenWidth/2, screenHeight/2, button='left')
pyautogui.press('shift') # 切换输入法的中英文
pyautogui.press(['#', ' ']) # press 可以对单个字符或者列表进行操作
pyautogui.press(['w', 'e', 'i'])
pyautogui.press(['l', 'a', 'n'])
pyautogui.press(' ')

pyautogui.hotkey('shift', 'a') # 可以使用组合键

 

posted @ 2022-01-21 12:10  dingningcnblogs  阅读(653)  评论(0)    收藏  举报