1、安装模块: 在Windows 上,不需要安装其他模块。 在OS X 上,运行sudo pip3 install pyobjc-framework-Quartz,sudo pip3 install pyobjc-core,然后sudo pip3 install pyobjc。 在Linux 上,运行sudo pip3 install python3-xlib,sudo apt-get install scrot,sudo apt-get install python3-tk,以及sudo apt-get install python3-dev(Scrot 是 PyAutoGUI 使用的屏幕快照程序)。 在这些依赖安装后,运行pip install pyautogu(i 或在OS X和Linux上运行pip3), 安装pyautogui。 2、pyautogui执行时,如果鼠标移到屏幕左上角,将导致pyautogui产生pyautogui.FailSafeException异常。如果设置FAILSAEF=False将禁止这项功能。
import pyautogui pyautogui.PAUSE = 1 pyautogui.FAILSAFE = True 这里我们导入pyautogui,并将pyautogui.PAUSE 设置为1,即每次函数调用后 暂停一秒。将pyautogui.FAILSAFE 设置为True,启动自动防故障功能。
获取屏幕分辨率
import pyautogui pyautogui.size() (1920, 1080) width, height = pyautogui.size() 移动鼠标,moveTo,duration指定移动的时间 import pyautogui for i in range(10): pyautogui.moveTo(100, 100, duration=0.25) pyautogui.moveTo(200, 100, duration=0.25) pyautogui.moveTo(200, 200, duration=0.25) pyautogui.moveTo(100, 200, duration=0.25) 从当前位置移动鼠标moveRel import pyautogui for i in range(10): pyautogui.moveRel(100, 0, duration=0.25) pyautogui.moveRel(0, 100, duration=0.25) pyautogui.moveRel(-100, 0, duration=0.25) pyautogui.moveRel(0, -100, duration=0.25) 获取鼠标位置:pyautogui.position() 3、实时显示鼠标当前位置: import pyautogui print('Press Ctrl-C to quit.') try: while True: # Get and print the mouse coordinates. x, y = pyautogui.position() positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4) print(positionStr, end='') print('\b' * len(positionStr), end='', flush=True)
except KeyboardInterrupt: print('\nDone.')
点击鼠标左键:pyautogui.click(100,150,button='left') pyautogui.click(200,250,button='right') pyautogui. mouseDown(),pyautogui.mouseUp(), pyautogui.doubleClick()双击鼠标左键 pyautogui.rightClick() 和pyautogui.middleClick() 右键和中建双击
拖动鼠标: import pyautogui, time time.sleep(5) pyautogui.click() # click to put drawing program in focus distance = 200 while distance > 0: pyautogui.dragRel(distance, 0, duration=0.2) # move right distance = distance - 5 pyautogui.dragRel(0, distance, duration=0.2) # move down pyautogui.dragRel(-distance, 0, duration=0.2) # move left distance = distance - 5 pyautogui.dragRel(0, -distance, duration=0.2) # move up
pyautogui.scroll(200) 滚动鼠标 屏幕快照:
import pyautogui im = pyautogui.screenshot() im.getpixel((0, 0)) (176, 176, 175) im.getpixel((50, 200)) (130, 135, 144)
像素比较:pyautogui.pixelMatchesColor(50, 200, (130, 135, 144)) 实时显示鼠标位置和像素:
import pyautogui
print('Press Ctrl-C to quit.')
try:
while True:
# Get and print the mouse coordinates.
x, y = pyautogui.position()
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
pixelColor = pyautogui.screenshot().getpixel((x, y))
positionStr += ' RGB: (' + str(pixelColor[0]).rjust(3)
positionStr += ', ' + str(pixelColor[1]).rjust(3)
positionStr += ', ' + str(pixelColor[2]).rjust(3) + ')'
print(positionStr, end='')
print('\b' * len(positionStr), end='', flush=True)
except KeyboardInterrupt:
print('\nDone.')
获取图像位置:locateOnScreen
import pyautogui pyautogui.locateOnScreen('submit.png') (643, 745, 70, 29)
list(pyautogui.locateAllOnScreen('submit.png')) [(643, 745, 70, 29), (1007, 801, 70, 29)] 获取中心坐标:center pyautogui.locateOnScreen('submit.png') (643, 745, 70, 29) pyautogui.center((643, 745, 70, 29)) (678, 759) pyautogui.click((678, 759))
打字:typewrite pyautogui.click(100, 100); pyautogui.typewrite('Hello world!')
pyautogui.typewrite(['a', 'b', 'left', 'left', 'X', 'Y'])
typewrite键列表 'a', 'b', 'c', 'A', 'B', 'C', '1', '2', '3', '!', '@', '#',等等 单个字符的键 'enter'(or 'return' or '\n') 回车键 'esc' Esc 键 'shiftleft', 'shiftright' 左右Shift 键 'altleft', 'altright' 左右Alt 键 'ctrlleft', 'ctrlright' 左右Ctrl 键 'tab'(or '\t') Tab 键 'backspace', 'delete' Backspace 和Delete 键 'pageup', 'pagedown' Page Up 和Page Down 键 'home', 'end' Home 和End 键 'up', 'down', 'left', 'right' 上下左右箭头键 'f1', 'f2', 'f3',等等 F1 至F12 键 'volumemute', 'volumedown', 'volumeup' 静音、减小音量、放大音量键(有些键盘没有这些键,但你 的操作系统仍能理解这些模拟的按键) 'pause' Pause 键 'capslock', 'numlock', 'scrolllock' Caps Lock,Num Lock 和Scroll Lock 键 'insert' Ins 或Insert 键 'printscreen' Prtsc 或Print Screen 键 'winleft', 'winright' 左右Win 键(在Windows 上) 'command' Command 键(在OS X 上) 'option' Option 键(在OS X 上)
按下键 pyautogui.keyDown() 和pyautogui.keyUp()
组合键 pyautogui.keyDown('shift'); pyautogui.press('4'); pyautogui.keyUp('shift') 热键组合:Hotkey pyautogui.hotkey('ctrl', 'c')