pip3 install pyautogui
import pyautogui
# 获取当前屏幕分辨率
screenWidth, screenHeight = pyautogui.size()
print(screenWidth)
print(screenHeight)
import pyautogui
# 获取当前屏幕分辨率
screenWidth, screenHeight = pyautogui.size()
# 获取当前鼠标位置
currentMouseX, currentMouseY = pyautogui.position()
print(currentMouseX)
print(currentMouseY)
# 5秒钟鼠标从当前位置移动到坐标为100,100的位置
pyautogui.moveTo(100, 100,5)
#鼠标立即移到屏幕中央
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)
#5秒钟鼠标从当前位置向下移动100,向右移动100
pyautogui.moveRel(xOffset=100, yOffset=100,duration=5)
# 用缓动/渐变函数让鼠标2秒后移动到(500,500)位置
pyautogui.moveTo(x=500, y=500, duration=2, tween=pyautogui.easeInOutQuad)
import pyautogui
# 函数参数
# x
# y
# 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)
# 鼠标当前位置0间隔双击
pyautogui.doubleClick(x=None, y=None, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)
# 鼠标当前位置3击
pyautogui.tripleClick(x=None, y=None, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)
#右击
pyautogui.rightClick()
#中击
pyautogui.middleClick()
import pyautogui
#3秒鼠标按下左键从当前位置拖拽到400,500
pyautogui.dragTo(x=400, y=500, duration=3,button='left')
#3秒鼠标按下左键从当前位置拖拽相对移动100,100
pyautogui.dragRel(xOffset=100,yOffset=100,duration=3,button='left',mouseDownUp=True)
import pyautogui
#鼠标移动到x=1796, y=778位置按下
pyautogui.mouseDown(x=1796, y=778, button='left')
#鼠标移动到x=2745, y=778位置松开(与mouseDown组合使用选中)
pyautogui.mouseUp(x=2745, y=778, button='left',duration=5)
import pyautogui
#鼠标当前位置滚轮滚动500距离
pyautogui.scroll(500)
#鼠标水平滚动(Linux)
pyautogui.hscroll(500)
#鼠标左右滚动(Linux)
pyautogui.vscroll(500)
import pyautogui
#模拟输入信息,每个输入字母间隔0.5秒
pyautogui.typewrite(message='Hello world!',interval=0.5)
#点击ESC
pyautogui.press('esc')
# 按住shift键
pyautogui.keyDown('shift')
# 放开shift键
pyautogui.keyUp('shift')
# 模拟组合热键
pyautogui.hotkey('ctrl', 'c')
#点击windows键
pyautogui.press('winleft')
按键支持
按键 | 说明 |
---|---|
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…. | F1…….F12键 |
volumemute, volumedown,volumeup | 有些键盘没有 |
pause | PAUSE键 |
capslock, numlock,scrolllock | CAPS LOCK, NUM LOCK, 和 SCROLLLOCK 键 |
insert | INS或INSERT键 |
printscreen | PRTSC 或 PRINT SCREEN键 |
winleft, winright | Win键 |
command | Mac OS X command键 |
import pyautogui
pyautogui.alert(text='This is an alert box.', title='Test')
import pyautogui
#pyautogui.confirm('Shall I proceed?')
pyautogui.confirm('Enter option.', buttons=['A', 'B', 'C'])
输入数据会被隐藏,以“*”代替显示
import pyautogui
a = pyautogui.password('Enter password (text will be hidden)')
print(a)
import pyautogui
a = pyautogui.prompt('input message')
print(a)
需要安装Pillow库
import pyautogui
img_path = r'D:\log\one.png'
im1 = pyautogui.screenshot()
im1.save('my_screenshot.png')
im2 = pyautogui.screenshot('my_screenshot2.png')
#只截取部分区域
im = pyautogui.screenshot(region=(0,0, 300, 400))
im.save(img_path)
import pyautogui
#在当前屏幕中查找指定图片(图片需要由系统截图功能截取的图)
coords = pyautogui.locateOnScreen('folder.png')
#获取定位到的图中间点坐标
x,y=pyautogui.center(coords)
#右击该坐标点
pyautogui.rightClick(x,y)
import pyautogui
#保护措施,把鼠标移动到屏幕左上角能够强制停止pyautogui,避免失控。
pyautogui.FAILSAFE = True
#为所有的PyAutoGUI函数增加延迟,即执行完一个函数等待0.5秒再执行下一个。默认延迟时间是0.1秒。
pyautogui.PAUSE = 0.5
参考:
https://blog.csdn.net/weixin_43430036/article/details/84650938
https://www.cnblogs.com/dcb3688/p/4607980.html