PyautoGui Common Tutorial of Mastery
- 2021-10-27 07:59:55
- OfStack
2. Mouse parameters
3. Keyboard events
4. Screen processing
Read a lot of Pyautogui, essence summary, you want to have here, one is enough!
1. Pre-parameters
Automatic fail-safe function
# The default function is True, This function means that when the mouse pointer is at the top of the screen, the program will report an error; The purpose is to prevent the program from stopping
pyautogui.FAILSAFE =False
Pause function
# Means all pyautogui The instructions of must be suspended 1 Seconds; Other instructions will not stop; By doing so, you can prevent the keyboard and mouse from operating too fast;
pyautogui.PAUSE = 1
2. Mouse parameters
2.1 Get screen resolution
print(pyautogui.size()) # Returns the resolution of the display used; Output: Size(width=1920, height=1080)
width,height = pyautogui.size()
print(width,height) # 1920 1080
2.2 Mouse movement events
Move to the specified position
# Move the mouse to the specified coordinates; duration The function of is to set the movement time, all of which gui All functions have this parameter, and they are all optional parameters;
# Move to the right 100px , move down 500px, This process continues 1 Seconds;
pyautogui.moveTo(100,300,duration=1)
Mouse position
print(pyautogui.position()) # Get the current mouse position; Output: Point(x=200, y=800)
2.3 Mouse Click Events
Mouse click
# Click the mouse
pyautogui.click(10,10) # Click the specified location with the mouse, and the default left key
pyautogui.click(10,10,button='left') # Left-click
pyautogui.click(1000,300,button='right') # Right-click
pyautogui.click(1000,300,button='middle') # Click Middle
Double-click the mouse
pyautogui.doubleClick(10,10) # Specify the location, double-click the left key
pyautogui.rightClick(10,10) # Specify the location and double-right click
pyautogui.middleClick(10,10) # Specify the position and double-hit the key
Mouse click and release
pyautogui.mouseDown() # Mouse press
pyautogui.mouseUp() # Mouse release
Mouse control drag event
# Drag to the specified location
# Drag the mouse to the specified coordinates; duration The function of is to set the movement time, all of which gui All functions have this parameter, and they are all optional parameters
pyautogui.dragTo(100,300,duration=1)
# Drag in Direction
# Drag to the right 100px Drag down 500px, This process continues 1 Seconds
pyautogui.dragRel(100,500,duration=4) # No. 1 1 The parameter is to move the pixel value left and right, the first 2 One is up and down
Mouse wheel
# The functions that control mouse scrolling are scroll() , Incoming 1 An integer parameter indicating how many units to scroll up or down; Units vary depending on the operating system
pyautogui.scroll(300) # Scroll down 300 Units;
3. Keyboard events
3.1 Keyboard input function
pyautogui. keyDown (): Analog key press; pyautogui. keyUp (): Analog key release; pyautogui. press (): # Is to call keyDown () & keyUp (), simulating the key once; pyautogui. typewrite ('this', 0.5): The first parameter is the input content, and the second parameter is the interval between each character; pyautogui. typewrite (['T', 'h', 'i', 's']): typewrite can also pass in a single-letter list;
Keyboard event
# Means all pyautogui The instructions of must be suspended 1 Seconds; Other instructions will not stop; By doing so, you can prevent the keyboard and mouse from operating too fast;
pyautogui.PAUSE = 1
0
Slow output
# Means all pyautogui The instructions of must be suspended 1 Seconds; Other instructions will not stop; By doing so, you can prevent the keyboard and mouse from operating too fast;
pyautogui.PAUSE = 1
1
3.2 Keyboard Output in Special Sequence
# Means all pyautogui The instructions of must be suspended 1 Seconds; Other instructions will not stop; By doing so, you can prevent the keyboard and mouse from operating too fast;
pyautogui.PAUSE = 1
2
3.3 Special Keys
键盘字符串 | 说明 |
---|---|
enter(或return 或 \n) | 回车 |
esc | ESC键 |
shiftleft, shiftright | 左右SHIFT键 |
altleft, altright | 左右ALT键 |
ctrlleft, ctrlright | 左右CTRL键 |
tab (\t) | TAB键 |
backspace, delete | BACKSPACE 、DELETE键 |
pageup, pagedown | PAGE UP 和 PAGE DOWN键 |
home, end | HOME 和 END键 |
up, down, left,right | 箭头键 |
f1, f2, f3…. f12 | F1…….F12键 |
volumemute, volumedown,volumeup | 声音变大变小静音(有些键盘没有) |
pause | PAUSE键,暂停键 |
capslock | CAPS LOCK 键 |
numlock | NUM LOCK 键 |
scrolllock | SCROLLLOCK 键 |
insert | INSERT键 |
printscreen | PRINT SCREEN键 |
winleft, winright | Win键(windows ) |
command | command键(Mac OS X ) |
option | option(Mac OS X) |
3.4 Shortcut Keys
If we need the shortcut key ctrl + c to simulate replication, if we use the previous method, the code is:
pyautogui.keyDown('ctrl')
pyautogui.keyDown('c')
pyautogui.keyUp('c')
pyautogui.keyUp('ctrl')
After optimization
# Means all pyautogui The instructions of must be suspended 1 Seconds; Other instructions will not stop; By doing so, you can prevent the keyboard and mouse from operating too fast;
pyautogui.PAUSE = 1
4
4. Screen processing
4.1 Screenshot
pyautogui provides a method screenshot () that returns an image object for an Pillow
Common parameters
# Means all pyautogui The instructions of must be suspended 1 Seconds; Other instructions will not stop; By doing so, you can prevent the keyboard and mouse from operating too fast;
pyautogui.PAUSE = 1
5
Screenshot operation
# Means all pyautogui The instructions of must be suspended 1 Seconds; Other instructions will not stop; By doing so, you can prevent the keyboard and mouse from operating too fast;
pyautogui.PAUSE = 1
6