import pyautogui
import time
import pyautogui
pyautogui.PAUSE = 1 #default 0.1s
pyautogui.FAILSAFE = True #default True
Return tuple of integer: (width, height) for size of your main monitor
pyautogui.size()
Check if XY coordinates are on the screen
pyautogui.onScreen(x, y)
import pyautogui
print(pyautogui.size())
pyautogui.onScreen(0,1000)
(1440, 900)
False
width, height = pyautogui.size()
pyautogui.moveTo(x, y, duration=seconds)
#If 0, then the mouse cursor is moved instantaneously. 0.0 by default.
pyautogui.moveRel(xOffset, yOffset, duration=seconds)
for i in range(5):
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)
for i in range(5):
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()
pyautogui.position()
(326, 223)
import pyautogui
print('Press Ctrl-C to quit.')
Press Ctrl-C to quit.
try:
while True:
# TODO: Get and print the mouse coordinates.
except KeyboardInterrupt:
print('\nDone.')
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)
from IPython.display import display, clear_output
import pyautogui
import time
try:
while True:
# TODO: Get and print the mouse coordinates.
x, y = pyautogui.position()
positionStr = 'X: ' + str(x).rjust(4) + ', Y: ' + str(y).rjust(4)
display(positionStr)
time.sleep(0.2)
clear_output(wait=True)
except:
print('\nDone.')
pyautogui.click(x, y, button='left')
pyautogui.mouseDown(x, y, button='left')
pyautogui.mouseUp(x, y, button='left')
# or multiple clicks
pyautogui.click(clicks=3, interval=0.25, button='right')
pyautogui.doubleClick(x, y)
import pyautogui
pyautogui.click(400, 300)
import pyautogui
pyautogui.mouseDown(400, 300)
pyautogui.mouseUp(400, 300)
pyautogui.dragTo(x, y, duration=2)
pyautogui.dragRel(xOffset, yOffset, duration=0.25, button='right')
### demo
import pyautogui
#drag google chrome icon in dock
pyautogui.moveTo(910, 880)
pyautogui.dragTo(985, 880, duration=2)
pyautogui.dragRel(-75, 0, duration=2)
import pyautogui, time
time.sleep(5)
pyautogui.click()
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
import pyautogui
import time
pyautogui.moveRel(0,200, duration=1)
for i in range(3):
time.sleep(2)
pyautogui.scroll(-3)
pyautogui.screenshot()
pyautogui.screenshot(region=(left, top, width, height))
#(left, top) is the upper left corner
import pyautogui, time
import os
time.sleep(2)
im = pyautogui.screenshot(imageFilename='screenshot.png',region=(360, 225, 500, 450))
#current directory
print(os.getcwd())
im
/Users/nancy/pyladies
im.getpixel((0, 0))
(41, 36, 48, 255)
pyautogui.pixelMatchesColor(x, y, (R, G, B))
time.sleep(1)
pyautogui.pixelMatchesColor(360, 225, (41, 36, 48))
True
#! python
import pyautogui
print('Press Ctrl-C to quit.')
try:
while True:
# TODO: Get and print the mouse coordinates.
x, y = pyautogui.position()
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
# Add Code Here!!!!!
# Add Code Here!!!!!
# Add Code Here!!!!!
# Add Code Here!!!!!
print(positionStr, end='')
print('\b' * len(positionStr), end='', flush=True)
except KeyboardInterrupt:
print('\nDone.')
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) + ')'
import pyautogui
import time
time.sleep(2)
pyautogui.locateOnScreen('Img/screenshot.png')
#Returns (left, top, width, height) coordinate of first found instance of the image on the screen.
(10, 30, 600, 400)
time.sleep(1)
print(pyautogui.locateAllOnScreen('Img/screenshot.png'))
list(pyautogui.locateAllOnScreen('Img/screenshot.png'))
<generator object _locateAll_python at 0x11442bbf8>
[(10, 30, 600, 400)]
pyautogui.typewrite(string, seconds)
import time, pyautogui
time.sleep(10)
pyautogui.click(100, 100)
pyautogui.typewrite('Hello world!', 0.25)
pyautogui.typewrite(['KeyName',...], seconds)
Keyboard Key String | Meaning |
---|---|
'a', 'b', 'c', 'A', 'B', 'C', '1', '2', '3', '!', '@', '#', and so on | The keys for single characters |
'left' | The left arrow keys |
import time, pyautogui
time.sleep(10)
pyautogui.click(100, 100)
pyautogui.typewrite(['O', 'l', 'l', 'y', '!', '!', 'left', 'left', ' ', ',', 'H', 'I'], 0.25)
Keyboard Key String | Meaning |
---|---|
'a', 'b', 'c', 'A', 'B', 'C', '1', '2', '3', '!', '@', '#', and so on | The keys for single characters |
'enter' (or 'return' or '\n') | The ENTER key |
'esc' | The ESC key |
'shiftleft', 'shiftright' | The left and right SHIFT keys |
'altleft', 'altright' | The left and right ALT keys |
'ctrlleft', 'ctrlright' | The left and right CTRL keys |
'tab' (or '\t') | The TAB key |
'backspace', 'delete' | The BACKSPACE and DELETE keys |
'pageup', 'pagedown' | The PAGE UP and PAGE DOWN keys |
'home', 'end' | The HOME and END keys |
'up', 'down', 'left', 'right' | The up, down, left, and right arrow keys |
Keyboard Key String | Meaning |
---|---|
'f1', 'f2', 'f3', and so on | The F1 to F12 keys |
'volumemute', 'volumedown', 'volumeup' | The mute, volume down, and volume up keys (some keyboards do not have these keys, but your operating system will still be able to understand these simulated keypresses) |
'pause' | The PAUSE key |
'capslock', 'numlock', 'scrolllock' | The CAPS LOCK, NUM LOCK, and SCROLL LOCK keys |
'insert' | The INS or INSERT key |
'printscreen' | The PRTSC or PRINT SCREEN key |
'winleft', 'winright' | The left and right WIN keys (on Windows) |
'command' | The Command () key (on OS X) 'option' The OPTION key (on OS X) |
pyautogui.keyDown(key)
pyautogui.keyUpd(key)
import time, pyautogui
time.sleep(10)
pyautogui.click(500, 500)
pyautogui.keyDown('shift')
pyautogui.press('4')
pyautogui.keyUp('shift')
import pyautogui
pyautogui.keyDown('ctrl')
pyautogui.keyDown('a')
pyautogui.keyDown('c')
pyautogui.keyUp('c')
pyautogui.keyUp('a')
pyautogui.keyUp('ctrl')
import pyautogui
pyautogui.hotkey('ctrl', 'a', 'c')
import time, pyautogui, subprocess
subprocess.Popen(['open', '/Applications/Safari.app/'])
time.sleep(1)
pyautogui.typewrite('https://playoverwatch.com/zh-tw/', interval=0.25)
pyautogui.press('enter')
time.sleep(1)
x, y = pyautogui.size()
time.sleep(1)
pyautogui.click(x/2, y/2)
pyautogui.press(['up', 'up', 'down', 'down', 'left', 'right', 'left', 'right', 'b', 'a'], interval=0.5)