PyAutoGUI——让所有GUI都自动化
In [ ]:
import pyperclip
import pyautogui
# PyAutoGUI中文输入需要用粘贴实现
# Python 2版本的pyperclip提供中文复制
def paste(foo):
pyperclip.copy(foo)
pyautogui.hotkey('ctrl', 'v')
foo = u'学而时习之'
# 移动到文本框
pyautogui.click(130,30)
paste(foo)
In [ ]:
import pyautogui
screenWidth, screenHeight = pyautogui.size()
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)
In [ ]:
import pyautogui
screenWidth, screenHeight = pyautogui.size()
currentMouseX, currentMouseY = pyautogui.position()
pyautogui.moveTo(100, 150)
pyautogui.click()
# 鼠标向下移动10像素
pyautogui.moveRel(None, 10)
pyautogui.doubleClick()
# 用缓动/渐变函数让鼠标2秒后移动到(500,500)位置
# use tweening/easing function to move mouse over 2 seconds.
pyautogui.moveTo(1800, 500, duration=2, tween=pyautogui.easeInOutQuad)
# 在每次输入之间暂停0.25秒
pyautogui.typewrite('Hello world!', interval=0.25)
pyautogui.press('esc')
pyautogui.keyDown('shift')
pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])
pyautogui.keyUp('shift')
pyautogui.hotkey('ctrl', 'c')
In [ ]:
distance = 200
while distance > 0:
pyautogui.dragRel(distance, 0, duration=0.5) # 向右
distance -= 5
pyautogui.dragRel(0, distance, duration=0.5) # 向下
pyautogui.draIn gRel(-distance, 0, duration=0.5) # 向左
distance -= 5
pyautogui.dragRel(0, -distance, duration=0.5) # 向上
In [ ]:
import pyautogui
pyautogui.FAILSAFE = False
In [ ]:
import pyautogui
pyautogui.PAUSE = 2.5
pyautogui.moveTo(100,100); pyautogui.click()
In [ ]:
import pyautogui
# 当前鼠标的坐标
pyautogui.position()
Out[ ]:
In [ ]:
# 当前屏幕的分辨率(宽度和高度)
pyautogui.size()
Out[ ]:
In [ ]:
# (x,y)是否在屏幕上
x, y = 122, 244
pyautogui.onScreen(x, y)
Out[ ]:
In [ ]:
import pyautogui
pyautogui.PAUSE = 2.5
In [ ]:
import pyautogui
pyautogui.FAILSAFE = True
In [ ]:
num_seconds = 1.2
# 用num_seconds秒的时间把光标移动到(x, y)位置
pyautogui.moveTo(x, y, duration=num_seconds)
# 用num_seconds秒的时间把光标的X轴(水平)坐标移动xOffset,
# Y轴(竖直)坐标向下移动yOffset。
xOffset, yOffset = 50, 100
pyautogui.moveRel(xOffset, yOffset, duration=num_seconds)
In [ ]:
pyautogui.click(x=moveToX, y=moveToY, clicks=num_of_clicks, interval=secs_between_clicks, button='left')
In [ ]:
pyautogui.rightClick(x=moveToX, y=moveToY)
pyautogui.middleClick(x=moveToX, y=moveToY)
pyautogui.doubleClick(x=moveToX, y=moveToY)
pyautogui.tripleClick(x=moveToX, y=moveToY)
In [ ]:
pyautogui.scroll(clicks=amount_to_scroll, x=moveToX, y=moveToY)
In [ ]:
pyautogui.mouseDown(x=moveToX, y=moveToY, button='left')
pyautogui.mouseUp(x=moveToX, y=moveToY, button='left')
In [ ]:
# 每次键入的时间间隔
secs_between_keys = 0.1
pyautogui.typewrite('Hello world!\n', interval=secs_between_keys)
In [ ]:
pyautogui.typewrite(['a', 'b', 'c', 'left', 'backspace', 'enter', 'f1'], interval=secs_between_keys)
In [ ]:
pyautogui.KEYBOARD_KEYS[:10]
Out[ ]:
In [ ]:
pyautogui.hotkey('ctrl', 'a') # 全选
pyautogui.hotkey('ctrl', 'c') # 复制
pyautogui.hotkey('ctrl', 'v') # 粘贴
In [ ]:
pyautogui.keyDown(key_name)
pyautogui.keyUp(key_name)
In [ ]:
pyautogui.alert('这个消息弹窗是文字+OK按钮')
pyautogui.confirm('这个消息弹窗是文字+OK+Cancel按钮')
pyautogui.prompt('这个消息弹窗是让用户输入字符串,单击OK')
Out[ ]:
In [ ]:
# 返回一个Pillow/PIL的Image对象
pyautogui.screenshot()
pyautogui.screenshot('foo.png')
In [ ]:
# 返回(最左x坐标,最顶y坐标,宽度,高度)
pyautogui.locateOnScreen('pyautogui/looks.png')
Out[ ]:
In [ ]:
for i in pyautogui.locateAllOnScreen('pyautogui/looks.png'):
print(i)
In [ ]:
list(pyautogui.locateAllOnScreen('pyautogui/looks.png'))
Out[ ]:
In [ ]:
pyautogui.locateCenterOnScreen('pyautogui/looks.png')
Out[ ]:
In [ ]:
pyautogui.size()
Out[ ]:
In [ ]:
pyautogui.position()
Out[ ]:
In [ ]:
# ! python 3
import pyautogui
print('Press Ctrl-C to quit')
try:
while True:
x, y = pyautogui.position()
positionStr = 'X: {} Y: {}'.format(*[str(x).rjust(4) for x in [x, y]])
print(positionStr, end='')
print('\b' * len(positionStr), end='', flush=True)
except KeyboardInterrupt:
print('\n')
In [ ]:
# ! python
import pyautogui, sys
print('Press Ctrl-C to quit.')
try:
while True:
x, y = pyautogui.position()
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
print positionStr,
print '\b' * (len(positionStr) + 2),
sys.stdout.flush()
except KeyboardInterrupt:
print '\n'
In [ ]:
import pyautogui
pyautogui.onScreen(0, 0)
Out[ ]:
In [ ]:
pyautogui.onScreen(0, -1)
Out[ ]:
In [ ]:
pyautogui.onScreen(0, 2080)
Out[ ]:
In [ ]:
pyautogui.onScreen(1920, 1080)
Out[ ]:
In [ ]:
pyautogui.onScreen(1919, 1079)
Out[ ]:
In [ ]:
pyautogui.moveTo(100, 200) # 光标移动到(100, 200)位置
pyautogui.moveTo(None, 500) # 光标移动到(100, 500)位置
pyautogui.moveTo(600, None) # 光标移动到(600, 500)位置
In [ ]:
pyautogui.moveTo(100, 200, duration=2) # 用2秒把光标移动到(100, 200)位置
In [ ]:
pyautogui.moveTo(100, 200) #把光标移动到(100, 200)位置
pyautogui.moveRel(0, 50) #向下移动50
pyautogui.moveRel(30, 0, 2) #向右移动30
pyautogui.moveRel(30, None) #向右移动30
In [ ]:
# 按住鼠标左键,把鼠标拖拽到(100, 200)位置
pyautogui.dragTo(100, 200, button='left')
# 按住鼠标左键,用2秒钟把鼠标拖拽到(300, 400)位置
pyautogui.dragTo(300, 400, 2, button='left')
# 按住鼠标右键,用2秒钟把鼠标拖拽到(30,0)位置
pyautogui.dragTo(30, 0, 2, button='right')
In [ ]:
# 开始很慢,不断加速
pyautogui.moveTo(100, 100, 2, pyautogui.easeInQuad)
# 开始很快,不断减速
pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad)
# 开始和结束都快,中间比较慢
pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad)
# 一步一徘徊前进
pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce)
# 徘徊幅度更大,甚至超过起点和终点
pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic)
In [ ]:
pyautogui.click()
In [ ]:
# 先移动到(100, 200)再单击
pyautogui.click(x=100, y=200, duration=2)
In [ ]:
pyautogui.click(button='right')
In [ ]:
# 双击左键
pyautogui.click(clicks=2)
# 两次单击之间停留0.25秒
pyautogui.click(clicks=2, interval=0.25)
# 三击右键
pyautogui.click(button='right', clicks=2, interval=0.25)
In [ ]:
# 鼠标左键按下再松开
pyautogui.mouseDown(); pyautogui.mouseUp()
# 按下鼠标右键
pyautogui.mouseDown(button='right')
# 移动到(100, 200)位置,然后松开鼠标右键
pyautogui.mouseUp(button='right', x=100, y=200)
In [ ]:
# 向上滚动10格
pyautogui.scroll(10)
# 向下滚动10格
pyautogui.scroll(-10)
# 移动到(100, 100)位置再向上滚动10格
pyautogui.scroll(10, x=100, y=100)
In [ ]:
# 向右滚动10格
pyautogui.hscroll(10)
# 向左滚动10格
pyautogui.hscroll(-10)
In [ ]:
# 输入Hello world!
pyautogui.typewrite('Hello world!')
# 每次输入间隔0.25秒,输入Hello world!
pyautogui.typewrite('Hello world!', interval=0.25)
In [ ]:
# ENTER键
pyautogui.press('enter')
# F1键
pyautogui.press('f1')
# 左方向键
pyautogui.press('left')
In [ ]:
# 按下`shift`键
pyautogui.keyDown('shift')
pyautogui.press('left')
pyautogui.press('left')
pyautogui.press('left')
# 松开`shift`键
pyautogui.keyUp('shift')
In [ ]:
pyautogui.press(['left', 'left', 'left'])
In [ ]:
pyautogui.hotkey('ctrl', 'shift', 'ese')
In [ ]:
pyautogui.keyDown('ctrl')
pyautogui.keyDown('shift')
pyautogui.keyDown('esc')
pyautogui.keyUp('esc')
pyautogui.keyUp('shift')
pyautogui.keyUp('ctrl')
In [ ]:
print(pyautogui.KEYBOARD_KEYS)
In [ ]:
pyautogui.alert(text='', title='', button='OK')
Out[ ]:
In [ ]:
# OK和Cancel按钮的消息弹窗
pyautogui.confirm(text='', title='', buttons=['OK', 'Cancel'])
# 10个按键0-9的消息弹窗
pyautogui.confirm(text='', title='', buttons=range(10))
Out[ ]:
In [ ]:
pyautogui.prompt(text='', title='' , default='')
In [ ]:
pyautogui.password(text='', title='', default='', mask='*')
In [ ]:
import pyautogui
im1 = pyautogui.screenshot()
im2 = pyautogui.screenshot('my_screenshot.png')
In [ ]:
im = pyautogui.screenshot(region=(0, 0, 300 ,400))
In [ ]:
import pyautogui
button7location = pyautogui.locateOnScreen('pyautogui/calc7key.png')
button7location
Out[ ]:
In [ ]:
button7x, button7y = pyautogui.center(button7location)
button7x, button7y
Out[ ]:
In [ ]:
pyautogui.click(button7x, button7y)
In [ ]:
import pyautogui
x, y = pyautogui.locateCenterOnScreen('pyautogui/calc7key.png')
pyautogui.click(x, y)
In [ ]:
for pos in pyautogui.locateAllOnScreen('pyautogui/calc7key.png'):
print(pos)
In [ ]:
list(pyautogui.locateAllOnScreen('pyautogui/calc7key.png'))
Out[ ]:
In [ ]:
import pyautogui
button7location = pyautogui.locateOnScreen('pyautogui/calc7key.png', grayscale=True)
button7location
Out[ ]:
In [ ]:
import pyautogui
im = pyautogui.screenshot()
im.getpixel((100, 200))
Out[ ]:
In [ ]:
pyautogui.pixel(100, 200)
Out[ ]:
In [ ]:
pyautogui.pixelMatchesColor(100, 200, (255, 255, 255))
Out[ ]:
In [ ]:
pyautogui.pixelMatchesColor(100, 200, (255, 255, 245))
Out[ ]:
In [ ]:
pyautogui.pixelMatchesColor(100, 200, (255, 255, 245), tolerance=10)
Out[ ]:
In [ ]:
pyautogui.pixelMatchesColor(100, 200, (248, 250, 245), tolerance=10)
Out[ ]:
In [ ]:
pyautogui.pixelMatchesColor(100, 200, (205, 255, 245), tolerance=10)
Out[ ]: