很简单的一个库
安装
与常规python库类似
pip install autopy
# 或:
pip install -U autopy
基础使用
官方tutorial
需要注意的是, 核心代码应该不是python代码(是.pyd), 所以在IDE中并不会提示参数, 使用时需要经常参照文档
autopy.alert
一个autopy用例:最简单的定时提醒工具
import autopy as at
import datetime
import time
while(True):
now = datetime.datetime.now()
fmt = '%y-%m-%d, %H:%M:%S'
if datetime.datetime(2019,11,7,19,46)<now:
at.alert.alert('now is '+ now.strftime(fmt))
break
time.sleep(1)
鼠标键盘操控
鼠标相关
import autopy, math
TWO_PI = 3.1416*2
autopy.mouse.move(x=1, y=1)
autopy.mouse.smooth_move(1, 1, 2)#最后参数为时间(秒)
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()
键盘类似
值得注意的是由于未知原因, autopy.key.Code.TAB缺失导致无法打出该按键
截屏
编写特殊用途脚本的时候, 有时需要获取屏幕状态/特定点集的颜色
import autopy
scn = autopy.bitmap.capture_screen() #返回的是bitmap对象
color = scn.get_color(1, 1) #可读性低
print(hex(color))
print(autopy.color.hex_to_rgb(color))
scn.save(pathname)
scn = autopy.bitmap.Bitmap.open(pathname)
pos = scn2.find_bitmap(scn1) #模板匹配
pos = scn2.find_bitmap(scn1, rect=((10,10),(100,100)) # ((x, y), (width, height))