PyAutoGUI is a Python library that automatically controls the keyboard and mouse. But anything you don’t want to do manually can be done with this library.

For example, I want to send a regular wechat in the middle of the night, or automatically brush the page every day and other operations, it can completely simulate manual operation, and you can feel at ease to brush drama.

Well, laziness is a programmer’s virtue.

Install pyautogui

pip install pyautogui
Copy the code

Keyboard and mouse control

>>> import pyautogui
>>> screenWidth, screenHeight = pyautogui.size() # Return to screen resolution
>>> currentMouseX, currentMouseY = pyautogui.position() # return to mouse position
>>> pyautogui.moveTo(100.150) Move the mouse pointer to the specified position
>>> pyautogui.click() # click
>>> pyautogui.click(200.220) # Click the location
>>> pyautogui.move(None.10)  # Move the mouse 10 pixels
>>> pyautogui.doubleClick() # Double click
>>> pyautogui.write('Hello world! ', interval=0.25)  Enter a string with a retention time of 0.25 seconds per character
>>> pyautogui.press('esc') # exit key
>>> pyautogui.keyDown('shift')  # Shitf keyboard
>>> pyautogui.hotkey('ctrl'.'c')  # key combination
Copy the code

Draw automatically with PyAutoGUI

Display message box

In addition to controlling the mouse key tray, you can also call the system popover

>>> import pyautogui
>>> pyautogui.alert('This is an alert box.')
'OK'
>>> pyautogui.confirm('Shall I proceed? ')
'Cancel'
>>> pyautogui.confirm('Enter option.', buttons=['A'.'B'.'C'])
'B'
>>> pyautogui.prompt('What is your name? ')
'Al'
>>> pyautogui.password('Enter password (text will be hidden)')
'swordfish'
Copy the code

Some simple human-computer interaction can be realized through the window message box, for example, when some places need manual input, it can accept the user’s instructions.

screenshots

The screenshot is achieved using the Pillow module. The purpose of the screenshot is to recognize the content of the image through the image recognition technology, and then accurately locate the location of an element through the content to achieve precise clicking.

>>> import pyautogui
>>> im1 = pyautogui.screenshot()
>>> im1.save('my_screenshot.png')
>>> im2 = pyautogui.screenshot('my_screenshot2.png')
Copy the code

This article is published on the public account Of Python Zen, welcome to follow