Pyautogui is a library of Python that can simulate the operation of the mouse and keyboard to achieve automatic functions!
Installation :
pip install pyautogui
The installation can be successful under normal conditions. If the network condition is not good, you can set timeout and mirroring.
Import module
import pyautogui
Mouse operation function
function | Features |
---|---|
click() | Click the mouse at the current position |
position() | Get the current position of the mouse |
size() | Get screen resolution |
move() | Move the mouse a specified distance from the current position |
moveTo() | Move the mouse to the specified position |
dragTo () | Drag the mouse to the specified position |
dragRel () | Drag the mouse a specified distance from the current position |
mouseDown() | Mouse down |
mouseUp() | Release the mouse and use it in combination with mouseDown() |
def click(
x=None, y=None, clicks=1, interval=0.0, button=PRIMARY, duration=0.0, tween=linear, logScreenshot=None, _pause=True
)
# clicks:点击次数
# interval:点击之间的间隔
# button 'left', 'middle', 'right' 对应鼠标 左 中 右或者取值(1, 2, or 3)
# tween 渐变函数
pyautogui.click(x=None, y=None, clicks=1, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)
#可以直接简写为:
pyautogui.click()
x,y = pyautogui.position()
print ("当前鼠标的X轴的位置为:{},Y轴的位置为:{}".format(x,y))
x,y = pyautogui.size()
print ("当前屏幕的分辨率是{}*{}".format(x,y))
print(pyautogui.position())
pyautogui.move(50,100)
print(pyautogui.position())
pyautogui.moveTo(50, 100)
print(pyautogui.position())
#
# Point(x=824, y=471)
# Point(x=874, y=571)
# Point(x=50, y=100)
# 用缓动/渐变函数让鼠标2秒后移动到(500,500)位置
pyautogui.moveTo(x=500, y=500, duration=2, tween=pyautogui.easeInOutQuad)
#鼠标拖拽
pyautogui.dragTo(x=427, y=535, duration=3,button='left')
#鼠标相对拖拽
pyautogui.dragRel(xOffset=100,yOffset=100,duration=2,button='left',mouseDownUp=False)
#鼠标移动到x=1000, y=500位置按下
pyautogui.mouseDown(x=1000, y=500, button='left')
#鼠标移动到x=1500, y=600位置松开(与mouseDown组合使用选中)
pyautogui.mouseUp(x=1500, y=600, button='left',duration=5)
There are also some less commonly used functions:
#Double-click pyautogui.doubleClick()
#Three-click pyautogui.tripleClick()
#right-click pyautogui.rightClick()
#Middle-click pyautogui.middleClick() #Roller
x is regular and upward pyautogui.scroll(x)
Keyboard operation function
function | effect |
---|---|
typewrite() | Enter the specified content |
press() | Click the button |
keyDown() | Hold the key |
keyUp() | Release the button |
hotkey() | Combination keys |
#模拟输入信息
pyautogui.typewrite(message='Hello world!',interval=0.5)
#点击ESC
pyautogui.press('esc')
# 按住shift键
pyautogui.keyDown('shift')
# 放开shift键
pyautogui.keyUp('shift')
# 模拟组合热键
pyautogui.hotkey('win', 'up')
Prompt message:
alert:
pyautogui.alert(text='This is an alert ', title='Test')
option:
t=pyautogui.confirm('Enter option.', buttons=['A', 'B', 'C'])
print(t)
password:
a = pyautogui.password('Enter password (text will be hidden)')
print(a)
prompt:
a = pyautogui.prompt('input message')
print(a)
Take a screenshot and save:
im1 = pyautogui.screenshot()
im1.save('my_screenshot.png')
Big picture find small picture:
#在当前屏幕中查找指定图片(图片需要由系统截图功能截取的图)
coords = pyautogui.locateOnScreen('folder.png')
#获取定位到的图中间点坐标
x,y,width,height=pyautogui.center(coords)
print(x,y,width,height)