欢迎访问PyAutoGUI文档!¶
PyAutoGUI允许您的Python脚本控制鼠标和键盘,实现与其他应用程序的自动化交互。该API设计简洁易用。PyAutoGUI支持Windows、macOS和Linux系统,兼容Python 2和3版本。
要使用pip安装,请运行pip install pyautogui。更多详情请参阅Installation页面。
源代码可在以下地址获取:https://github.com/asweigart/pyautogui
PyAutoGUI具有以下功能特点:
- 在其他应用程序窗口中移动鼠标并点击。
- 向应用程序发送按键操作(例如填写表单)。
- 截取屏幕截图,并给定一张图片(例如按钮或复选框的图片),在屏幕上找到它。
- 定位应用程序窗口,并移动、调整大小、最大化、最小化或关闭它(目前仅限Windows系统)。
- 显示警报和消息框。
这里有一个YouTube视频展示了一个自动玩《回转寿司》游戏的机器人。该机器人会监视游戏应用窗口,搜索寿司订单的图像。当发现订单时,它会点击配料按钮来制作寿司。还会根据需要点击游戏中的电话订购更多食材。这个机器人完全自主运行,能够完成游戏中全部七天的任务。这正是PyAutoGUI能够实现的自动化功能。
示例¶
>>> import pyautogui
>>> screenWidth, screenHeight = pyautogui.size() # Get the size of the primary monitor.
>>> screenWidth, screenHeight
(2560, 1440)
>>> currentMouseX, currentMouseY = pyautogui.position() # Get the XY position of the mouse.
>>> currentMouseX, currentMouseY
(1314, 345)
>>> pyautogui.moveTo(100, 150) # Move the mouse to XY coordinates.
>>> pyautogui.click() # Click the mouse.
>>> pyautogui.click(100, 200) # Move the mouse to XY coordinates and click it.
>>> pyautogui.click('button.png') # Find where button.png appears on the screen and click it.
>>> pyautogui.move(400, 0) # Move the mouse 400 pixels to the right of its current position.
>>> pyautogui.doubleClick() # Double click the mouse.
>>> 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') # Press the Esc key. All key names are in pyautogui.KEY_NAMES
>>> with pyautogui.hold('shift'): # Press the Shift key down and hold it.
pyautogui.press(['left', 'left', 'left', 'left']) # Press the left arrow key 4 times.
>>> # Shift key is released automatically.
>>> pyautogui.hotkey('ctrl', 'c') # Press the Ctrl-C hotkey combination.
>>> pyautogui.alert('This is the message to display.') # Make an alert box appear and pause the program until OK is clicked.
这个示例演示了在MS Paint(或任何图形绘制程序)中让鼠标按方形螺旋轨迹移动:
>>> distance = 200
>>> while distance > 0:
pyautogui.drag(distance, 0, duration=0.5) # move right
distance -= 5
pyautogui.drag(0, distance, duration=0.5) # move down
pyautogui.drag(-distance, 0, duration=0.5) # move left
distance -= 5
pyautogui.drag(0, -distance, duration=0.5) # move up
与直接生成图像文件的脚本相比,使用PyAutoGUI的优势在于您可以利用MS Paint提供的画笔工具。
常见问题解答¶
问:PyAutoGUI能否在安卓、iOS或平板/智能手机应用上运行。
A: 很遗憾不行。PyAutoGUI 仅支持在 Windows、macOS 和 Linux 系统上运行。
问:PyAutoGUI是否支持多显示器设置。
A: 不,目前PyAutoGUI仅支持主显示器。
问:PyAutoGUI支持OCR功能吗?
A: 不,但这个功能已经在规划路线图中。
问:PyAutoGUI能否实现键盘记录功能,或者检测某个按键当前是否被按下?
A: 不,PyAutoGUI目前无法实现这个功能。
安全防护机制¶
就像《魔法师的学徒》中那些被施了魔法、不断往浴缸注水(最终导致水漫金山)的扫帚一样,程序中的一个错误可能让它失控。当鼠标光标自行移动时,很难再用鼠标来关闭程序。
作为一项安全特性,默认启用了故障保护功能。当调用PyAutoGUI函数时,如果鼠标位于主显示器四个角落中的任意一个,将会引发pyautogui.FailSafeException异常。每次调用PyAutoGUI函数后会有十分之一秒的延迟,以便用户有时间将鼠标猛推到角落触发故障保护。
您可以通过设置pyautogui.FAILSAFE = False来禁用这个安全保护机制。强烈建议您不要禁用安全保护功能。
十分之一秒的延迟由pyautogui.PAUSE设置控制,默认值为0.1。您可以修改此值。此外还有一个pyautogui.DARWIN_CATCH_UP_TIME设置,用于在macOS系统上的键盘和鼠标事件后添加额外延迟,因为操作系统在执行PyAutoGUI触发的事件后似乎需要缓冲时间。该值默认为0.01,即增加百分之一秒的额外延迟。
目录:
本文档仍在编写完善中。