一个用于 Python 的简单、跨平台的 GUI 自动化库。
项目描述
有关更多信息,请参阅GitHub 存储库。
AutoPy 介绍和教程
介绍
AutoPy 是一个用于 Python 的简单、跨平台的 GUI 自动化库。它包括控制键盘和鼠标、在屏幕上查找颜色和位图以及显示警报的功能。
目前在带有 XTest 扩展的 macOS、Windows 和 X11 上受支持。
入门
要求
- Python 2.7 或 Python 3.5 及更高版本。
- Rust 1.23.0-nightly 2019-02-06 或更高版本(除非使用二元轮分布)。
- macOS 10.6 及更高版本。
- Windows 7 及更高版本。
- 带有 XTest 扩展的 X11。
安装
首先,通过运行以下命令查看您的机器是否可以使用二元轮:
$ pip install -U autopy
如果失败,安装rustup然后运行:
$ rustup default nightly-2019-10-05
$ pip install -U setuptools-rust
$ pip install -U autopy
另一种选择是从 GitHub 存储库上的最新源构建:
$ git clone git://github.com/autopilot-rs/autopy-rs.git
$ cd autopy
$ make
$ make install
注意:AutoPy 目前需要2019-10-05
Rust nightly 才能从源代码构建。这是为了保持与旧版本 PyO3 的兼容性,因为最新版本已放弃对 Python 2 的支持。今年晚些时候,AutoPy 也可能会放弃对 Python 2 的支持,具体取决于升级到更新版本的 PyO3 或 Rust 的必要性。同时,在本地构建时,可能需要通过以下方式安装所需的 nightly:
rustup install nightly 2019-10-05 --force
这是由于 rustup 抱怨它不包含某些组件,例如rustfmt
.
在 Windows 上从源代码安装的其他说明可 在此处获得。
你好世界
以下是 autopy 中“hello world”脚本的来源。运行此代码将导致在每个主要平台上出现一个警报对话框:
import autopy
def hello_world():
autopy.alert.alert("Hello, world")
hello_world()
教程
控制鼠标
AutoPy 包含许多用于控制鼠标的函数。如需完整列表,请参阅API 参考。例如,立即将鼠标“传送”到屏幕的左上角:
>>> import autopy
>>> autopy.mouse.move(1, 1)
为了更真实地移动鼠标,我们可以使用:
>>> import autopy
>>> autopy.mouse.smooth_move(1, 1)
更好的是,我们可以编写自己的函数,以正弦波的形式在屏幕上移动鼠标:
import autopy
import math
import time
import random
import sys
TWO_PI = math.pi * 2.0
def sine_mouse_wave():
"""
Moves the mouse in a sine wave from the left edge of
the screen to the right.
"""
width, height = autopy.screen.size()
height /= 2
height -= 10 # Stay in the screen bounds.
for x in range(int(width)):
y = int(height * math.sin((TWO_PI * x) / width) + height)
autopy.mouse.move(x, y)
time.sleep(random.uniform(0.001, 0.003))
sine_mouse_wave()
控制键盘
下面将输入字符串“Hello, world!”中的键。在当前以 100 WPM 为重点的输入中:
import autopy
autopy.key.type_string("Hello, world!", wpm=100)
或者,可以使用以下方法输入单个键:
import autopy
autopy.key.tap(autopy.key.Code.TAB, [autopy.key.Modifier.META])
autopy.key.tap("w", [autopy.key.Modifier.META])
使用位图
所有 autopy 的位图例程都可以在模块中找到autopy.bitmap
。探索 autopy 的一个有用方法是使用 Python 的内置help()
函数,例如在help(autopy.bitmap.Bitmap)
. AutoPy 的函数是用描述性文档字符串记录的,所以这应该显示一个很好的概述。
>>> import autopy
>>> autopy.bitmap.capture_screen()
<Bitmap object at 0x12278>
这会截取主屏幕的屏幕截图,将其复制到位图,显示其内存地址,然后立即销毁它。让我们做一些更有用的事情,比如看看它的像素数据:
>>> import autopy
>>> autopy.bitmap.capture_screen().get_color(1, 1)
15921906
AutoPy 使用一个坐标系,其原点从左上角开始,所以这应该返回屏幕左上角像素的颜色。显示的数字看起来有点无法识别,但我们可以使用 Python 的内置hex
函数对其进行格式化:
>>> import autopy
>>> hex(autopy.bitmap.capture_screen().get_color(1, 1))
'0xF2F2F2'
或者,我们可以使用:
>>> import autopy
>>> autopy.color.hex_to_rgb(autopy.screen.get_color(1, 1))
(242, 242, 242)
它将该十六进制值转换为值的元组(r, g, b)
。(请注意
autopy.screen.get_color()
,此处使用的 只是 的更方便和有效的版本autopy.bitmap.capture_screen().get_color()
。)
要将屏幕截图保存到文件中,我们可以使用:
>>> import autopy
>>> autopy.bitmap.capture_screen().save('screengrab.png')
文件类型要么从文件名中自动解析,要么作为可选参数给出。目前仅支持 jpeg 和 png 文件。
>>> import autopy
>>> autopy.bitmap.Bitmap.open('needle.png')
<Bitmap object at 0x1001d5378>
除了分析位图的像素数据外,加载位图的主要用途是在屏幕上或另一个位图中找到它。例如,以下打印在位图中找到的第一张图像的坐标(从左到右,从上到下扫描):
import autopy
def find_image_example():
needle = autopy.bitmap.Bitmap.open('needle.png')
haystack = autopy.bitmap.Bitmap.open('haystack.png')
pos = haystack.find_bitmap(needle)
if pos:
print("Found needle at: %s" % str(pos))
find_image_example()
也可以通过传递一个元组来进行有界搜索((x, y), (width, height))
:
haystack.find_bitmap(needle, rect=((10, 10), (100, 100)))
使用 AutoPy 的项目
- AutoPyDriverServer , AutoPy 通过 WebDriver 或与 webdriver 兼容的服务器。
- guibot,一种使用各种计算机视觉和桌面控制后端的 GUI 自动化工具。
- spynner,具有 AJAX 支持的 Python 程序化网页浏览模块。
- SUMO,一个开源、高度便携、微观和连续的道路交通模拟包,旨在处理大型道路网络。
API 参考
希望你喜欢使用 autopy!有关更深入的概述,请参阅API 参考。
贡献
如果您对此项目感兴趣,请考虑投稿。您可以通过以下几种方式提供帮助:
执照
该项目根据您的选择在Apache-2.0或 MIT许可下获得许可。
除非您另有明确说明,否则按照 Apache-2.0 许可中的定义,您有意提交以包含在工作中的任何贡献均应如上所述获得双重许可,而无需任何附加条款或条件。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。