跳转到主要内容

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

项目描述

PyAutoGUI

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

pip安装pyautogui

完整文档可在https://pyautogui.readthedocs.org找到

简体中文文档可在https://github.com/asweigart/pyautogui/blob/master/docs/simplified-chinese.ipynb找到

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

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

依赖关系

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

Windows没有依赖。Win32扩展无需安装。

macOS需要安装pyobjc-core和pyobjc模块(顺序很重要)。

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安装这些模块

  • pyscreeze
  • pymsgbox
  • pytweening

示例用法

键盘和鼠标控制

PyAutoGUI使用的x,y坐标以屏幕左上角为原点。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 System。

支持

如果您觉得这个项目很有帮助,并想支持其开发,请考虑在Patreon上为其创作者捐款。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装软件包 的信息。

源分发

PyAutoGUI-0.9.54.tar.gz (61.2 kB 查看哈希值)

上传时间