Agent铲子-以编程方式轻松控制你的电脑

24 阅读2分钟

今天推荐一个很好用的工具pyautogui,是一个跨平台的 GUI 自动化 Python 模块,用于以编程方式控制鼠标和键盘。

pyautogui

文档:https://pyautogui.readthedocs.org
仓库:https://github.com/asweigart/pyautogui

01

安装

pip install pyautogui

支持Python2和Python3,macOS下还需要按顺序安装 pyobjc-core 和 pyobjc 模块,Linux 需要安装 python3-xlib模块,需要安装Pillow库

02

鼠标控制方法

鼠标位置

计算方式坐标是这样的:

import pyautogui

pyautogui.size() #(1920, 1080) 屏幕大小
pyautogui.position() #(187, 567) 鼠标位置

鼠标移动

时间控制/移动位置/移动像素

import pyautogui

pyautogui.moveTo(100, 200)   # moves mouse to X of 100, Y of 200.
pyautogui.moveTo(None, 500)  # moves mouse to X of 100, Y of 500.
pyautogui.moveTo(600, None)  # moves mouse to X of 600, Y of 500.

pyautogui.moveTo(100, 200, 2)   # moves mouse to X of 100, Y of 200 over 2 seconds

pyautogui.move(0, 50)       # move the mouse down 50 pixels.

鼠标点击(完整动作)

点击/在屏幕的位置点击/右键点击/连续点击多次

pyautogui.click()  # click the mouse
pyautogui.click(x=100, y=200)  # move to 100, 200, then click the left mouse button.
pyautogui.click(button='right')  # right-click the mouse
pyautogui.click(clicks=2)  # double-click the left mouse button

鼠标点击(分解动作)

点下去/释放/也可以和位置连续

pyautogui.mouseDown(); pyautogui.mouseUp()  # does the same thing as a left-button mouse click
pyautogui.mouseDown(button='right')  # press the right button down
pyautogui.mouseUp(button='right', x=100, y=200)  # move the mouse to 100, 200, then release the right button up.

03

键盘控制方法

keywords:

用多长时间写

pyautogui.write('Hello world!')                 # prints out "Hello world!" instantly
pyautogui.write('Hello world!', interval=0.25)  # prints out "Hello world!" with a quarter second delay after each character

压的过程,只压以及释放

pyautogui.keyDown('shift')  # hold down the shift key
pyautogui.press('left')     # press the left arrow key
pyautogui.keyUp('shift')    # release the shift key

多个keyword一起按

with pyautogui.hold('shift'):        
    pyautogui.press(['left', 'left', 'left'])

这个代码和以下代码效果一致

pyautogui.keyDown('shift')  # hold down the shift key
pyautogui.press('left')     # press the left arrow key
pyautogui.press('left')     # press the left arrow key
pyautogui.press('left')     # press the left arrow key
pyautogui.keyUp('shift')    # release the shift key

04

截屏

import pyautogui
im1 = pyautogui.screenshot()

05

Demo

import pyautogui
screenWidth, screenHeight = pyautogui.size()  # Returns two integers, the width and height of the screen. (The primary monitor, in multi-monitor setups.)
currentMouseX, currentMouseY = pyautogui.position()  # Returns two integers, the x and y of the mouse cursor's current position.
pyautogui.moveTo(100, 150)  # Move the mouse to the x, y coordinates 100, 150.
pyautogui.click()  # Click the mouse at its current location.
pyautogui.click(200, 220)  # Click the mouse at the x, y coordinates 200, 220.
pyautogui.move(None, 10)  # Move mouse 10 pixels down, that is, move the mouse relative to its current position.
pyautogui.doubleClick()  # Double click the mouse at the
pyautogui.moveTo(500, 500, duration=2,
                 tween=pyautogui.easeInOutQuad)  # Use tweening/easing function to move mouse over 2 seconds.
pyautogui.write('Hello world!', interval=0.25)  # Type with quarter-second pause in between each key.
pyautogui.press('esc')  # Simulate pressing the Escape key.
pyautogui.keyDown('shift')
pyautogui.write(['left', 'left', 'left', 'left', 'left', 'left'])
pyautogui.keyUp('shift')
pyautogui.hotkey('ctrl', 'c')

可以这些方式排列组合写一个demo,之后执行demo就可以在 自己的电脑屏幕上看到 和代码对应的操作了