Skip to main content

PyAutoGUI 允许 Python 控制鼠标和键盘以及其他 GUI 自动化任务。对于 Windows、macOS 和 Linux,在 Python 3 和 2 上。

项目描述

PyAutoGUI

PyAutoGUI 是一个面向人类的跨平台 GUI 自动化 Python 模块。用于以编程方式控制鼠标和键盘。

pip install pyautogui

完整文档可在https://pyautogui.readthedocs.org获得

简体中文文档在https://muxuezi.github.io/posts/doc-pyautogui.html

源代码在https://github.com/asweigart/pyautogui

如果您在安装 Python 时需要帮助,请访问https://installpython3.com/

依赖项

PyAutoGUI 支持 Python 2 和 3。如果您使用 pip 从 PyPI 安装 PyAutoGUI:

Windows 没有依赖项。不需要安装 Win32 扩展。

macOS 需要安装 rubicon-objc 模块(按此顺序)。

Linux 需要安装 python3-xlib(或 Python 2 的 python-xlib)模块。

需要安装 Pillow,在 Linux 上,您可能需要安装其他库以确保 Pillow 的 PNG/JPEG 正常工作。看:

https://stackoverflow.com/questions/7648200/pip-install-pil-e-tickets-1-no-jpeg-png-support

http://ubuntuforums.org/showthread.php?t=1751455

如果你想做开发并为 PyAutoGUI 做贡献,你需要从 PyPI 安装这些模块:

  • pyscreez
  • pymsgbox
  • pytweening

示例用法

键盘和鼠标控制

PyAutoGUI 使用的 x, y 坐标在屏幕左上角有 0, 0 原点坐标。x 坐标向右增加(就像在数学中一样),但 y 坐标向下增加(与数学相反)。在大小为 1920 x 1080 像素的屏幕上,坐标 0、0 表示左上角,而 1919、1079 表示右下角。

目前,PyAutoGUI 仅适用于主监视器。PyAutoGUI 对于第二台显示器的屏幕不可靠(鼠标功能可能会或可能不会在多显示器设置上工作,具体取决于您的操作系统和版本)。

PyAutoGUI 完成的所有键盘按下都会发送到当前具有焦点的窗口,就好像您按下了物理键盘键一样。

    >>> 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')

显示消息框

    >>> import pyautogui
    >>> pyautogui.alert('This is an alert box.')
    'OK'
    >>> pyautogui.confirm('Shall I proceed?')
    'Cancel'
    >>> pyautogui.confirm('Enter option.', buttons=['A', 'B', 'C'])
    'B'
    >>> pyautogui.prompt('What is your name?')
    'Al'
    >>> pyautogui.password('Enter password (text will be hidden)')
    'swordfish'

截图功能

(PyAutoGUI 使用 Pillow 来实现与图像相关的功能。)

    >>> import pyautogui
    >>> im1 = pyautogui.screenshot()
    >>> im1.save('my_screenshot.png')
    >>> im2 = pyautogui.screenshot('my_screenshot2.png')

您还可以找到图像在屏幕上的位置:

    >>> import pyautogui
    >>> button7location = pyautogui.locateOnScreen('button.png') # returns (left, top, width, height) of matching region
    >>> button7location
    (1416, 562, 50, 41)
    >>> buttonx, buttony = pyautogui.center(button7location)
    >>> buttonx, buttony
    (1441, 582)
    >>> pyautogui.click(buttonx, buttony)  # clicks the center of where the button was found

locateCenterOnScreen() 函数返回此匹配区域的中心:

    >>> import pyautogui
    >>> buttonx, buttony = pyautogui.locateCenterOnScreen('button.png') # returns (x, y) of matching region
    >>> buttonx, buttony
    (1441, 582)
    >>> pyautogui.click(buttonx, buttony)  # clicks the center of where the button was found

PyAutoGUI 是如何工作的?

三种主要操作系统(Windows、macOS 和 Linux)各有不同的方式来以编程方式控制鼠标和键盘。这通常会涉及令人困惑、晦涩和深刻的技术细节。PyAutoGUI 的工作是将所有这些复杂性隐藏在一个简单的 API 后面。

  • 在 Windows 上,PyAutoGUI 通过内置ctypes模块访问 Windows API(也称为 WinAPI 或 win32 API)。https://github.com/asweigart/nicewin上的nicewin模块演示了如何通过 Python 进行 Windows API 调用。

  • 在 macOS 上,PyAutoGUI 使用该rubicon-objc模块来访问 Cocoa API。

  • 在 Linux 上,PyAutoGUI 使用该Xlib模块来访问 X11 或 X Window 系统。