Debian Codegraph @

docs/quickstart.rst

raw
1 .. default-role:: code
2
3 ===========
4 Cheat Sheet
5 ===========
6
7 This is a quickstart reference to using PyAutoGUI. PyAutoGUI is cross-platform GUI automation module that works on Python 2 & 3. You can control the mouse and keyboard as well as perform basic image recognition to automate tasks on your computer.
8
9 **All the keyword arguments in the examples on this page are optional.**
10
11 >>> import pyautogui
12
13 PyAutoGUI works on Windows/Mac/Linux and on Python 2 & 3. Install from PyPI with `pip install pyautogui`.
14
15 General Functions
16 -----------------
17
18 >>> pyautogui.position() # current mouse x and y
19 (968, 56)
20 >>> pyautogui.size() # current screen resolution width and height
21 (1920, 1080)
22 >>> pyautogui.onScreen(x, y) # True if x & y are within the screen.
23 True
24
25 Fail-Safes
26 ----------
27
28 Set up a 2.5 second pause after each PyAutoGUI call:
29
30 >>> import pyautogui
31 >>> pyautogui.PAUSE = 2.5
32
33 When fail-safe mode is `True`, moving the mouse to the upper-left will raise a `pyautogui.FailSafeException` that can abort your program:
34
35 >>> import pyautogui
36 >>> pyautogui.FAILSAFE = True
37
38 Mouse Functions
39 ---------------
40
41 XY coordinates have 0, 0 origin at top left corner of the screen. X increases going right, Y increases going down.
42
43 >>> pyautogui.moveTo(x, y, duration=num_seconds) # move mouse to XY coordinates over num_second seconds
44 >>> pyautogui.moveRel(xOffset, yOffset, duration=num_seconds) # move mouse relative to its current position
45
46 If `duration` is 0 or unspecified, movement is immediate. Note: dragging on Mac can't be immediate.
47
48 >>> pyautogui.dragTo(x, y, duration=num_seconds) # drag mouse to XY
49 >>> pyautogui.dragRel(xOffset, yOffset, duration=num_seconds) # drag mouse relative to its current position
50
51 Calling `click()` just clicks the mouse once with the left button at the mouse's current location, but the keyword arguments can change that:
52
53 >>> pyautogui.click(x=moveToX, y=moveToY, clicks=num_of_clicks, interval=secs_between_clicks, button='left')
54
55 The `button` keyword argument can be `'left'`, `'middle'`, or `'right'`.
56
57 All clicks can be done with `click()`, but these functions exist for readability. Keyword args are optional:
58
59 >>> pyautogui.rightClick(x=moveToX, y=moveToY)
60 >>> pyautogui.middleClick(x=moveToX, y=moveToY)
61 >>> pyautogui.doubleClick(x=moveToX, y=moveToY)
62 >>> pyautogui.tripleClick(x=moveToX, y=moveToY)
63
64 Positive scrolling will scroll up, negative scrolling will scroll down:
65
66 >>> pyautogui.scroll(amount_to_scroll, x=moveToX, y=moveToY)
67
68 Individual button down and up events can be called separately:
69
70 >>> pyautogui.mouseDown(x=moveToX, y=moveToY, button='left')
71 >>> pyautogui.mouseUp(x=moveToX, y=moveToY, button='left')
72
73 Keyboard Functions
74 ------------------
75
76 Key presses go to wherever the keyboard cursor is at function-calling time.
77
78 >>> pyautogui.typewrite('Hello world!\n', interval=secs_between_keys) # useful for entering text, newline is Enter
79
80 A list of key names can be passed too:
81
82 >>> pyautogui.typewrite(['a', 'b', 'c', 'left', 'backspace', 'enter', 'f1'], interval=secs_between_keys)
83
84 The full list of key names is in `pyautogui.KEYBOARD_KEYS`.
85
86 Keyboard hotkeys like Ctrl-S or Ctrl-Shift-1 can be done by passing a list of key names to `hotkey()`:
87
88 >>> pyautogui.hotkey('ctrl', 'c') # ctrl-c to copy
89 >>> pyautogui.hotkey('ctrl', 'v') # ctrl-v to paste
90
91 Individual button down and up events can be called separately:
92
93 >>> pyautogui.keyDown(key_name)
94 >>> pyautogui.keyUp(key_name)
95
96
97 Message Box Functions
98 ---------------------
99
100 If you need to pause the program until the user clicks OK on something, or want to display some information to the user, the message box functions have similar names that JavaScript has:
101
102 >>> pyautogui.alert('This displays some text with an OK button.')
103 >>> pyautogui.confirm('This displays text and has an OK and Cancel button.')
104 'OK'
105 >>> pyautogui.prompt('This lets the user type in a string and press OK.')
106 'This is what I typed in.'
107
108 The `prompt()` function will return `None` if the user clicked Cancel.
109
110 Screenshot Functions
111 --------------------
112
113 PyAutoGUI uses Pillow/PIL for its image-related data.
114
115 On Linux, you must run `sudo apt-get install scrot` to use the screenshot features.
116
117 >>> pyautogui.screenshot() # returns a Pillow/PIL Image object
118 <PIL.Image.Image image mode=RGB size=1920x1080 at 0x24C3EF0>
119 >>> pyautogui.screenshot('foo.png') # returns a Pillow/PIL Image object, and saves it to a file
120 <PIL.Image.Image image mode=RGB size=1920x1080 at 0x31AA198>
121
122 If you have an image file of something you want to click on, you can find it on the screen with `locateOnScreen()`.
123
124 >>> pyautogui.locateOnScreen('looksLikeThis.png') # returns (left, top, width, height) of first place it is found
125 (863, 417, 70, 13)
126
127 The `locateAllOnScreen()` function will return a generator for all the locations it is found on the screen:
128
129 >>> for i in pyautogui.locateAllOnScreen('looksLikeThis.png')
130 ...
131 ...
132 (863, 117, 70, 13)
133 (623, 137, 70, 13)
134 (853, 577, 70, 13)
135 (883, 617, 70, 13)
136 (973, 657, 70, 13)
137 (933, 877, 70, 13)
138
139 >>> list(pyautogui.locateAllOnScreen('looksLikeThis.png'))
140 [(863, 117, 70, 13), (623, 137, 70, 13), (853, 577, 70, 13), (883, 617, 70, 13), (973, 657, 70, 13), (933, 877, 70, 13)]
141
142 The `locateCenterOnScreen()` function just returns the XY coordinates of the middle of where the image is found on the screen:
143
144 >>> pyautogui.locateCenterOnScreen('looksLikeThis.png') # returns center x and y
145 (898, 423)
146
147 These functions return `None` if the image couldn't be found on the screen.
148
149 Note: The locate functions are slow and can take a full second or two.
150