pyautogui操作汇总

1、鼠标控制

1.1 移动鼠标

1.1.1 鼠标移动到指定位置

# 移动到指定位置
# duration 移动所需的持续时间
pyautogui.move(100,200,duration=1)
pyautogui.moveTo(100,200,duration=1)

1.1.2 鼠标移动相对位置

pyautogui.moveRel(50,0,duration=0.5)

1.2 点击鼠标

# 点击当前位置
pyautogui.click()
# 点击指定坐标
pyautogui.click(x=100, y=200)
# 右击
pyautogui.click(button='right')
pyautogui.rightClick()
# 右击,clicks点击次数,interval每次点击的间隔
pyautogui.click(button='right', clicks=3, interval=0.25) 
pyautogui.tripleClick()
# 双击
pyautogui.doubleClick()


# 鼠标按下
pyautogui.mouseDown() 
# 鼠标释放
pyautogui.mouseUp()
# 鼠标右键按下
pyautogui.mouseDown(button='right')
# 鼠标拖动到指定坐标,并释放右键
pyautogui.mouseUp(button='right', x=100, y=200)

1.3 拖动鼠标

import pyautogui

# button: 指定哪个鼠标按钮用于拖动('left', 'middle', 'right')
# 拖动到指定坐标
pyautogui.dragTo(100, 200, duration=1, button='left')
# 拖动相对位置
pyautogui.dragRel(50, 50, duration=1, button='left')

1.4 滚动鼠标

# 滚动一定滚动量
pyautogui.scroll(10) 
pyautogui.scroll(10, x=100, y=100)

# 水平滚动
pyautogui.hscroll(10) 
# 垂直滚动
pyatuogui.vscroll(10)

2、键盘控制

# 按照一定速率输入内容
pyautogui.write('Hello world!', interval=0.25) 
# 摁下
pyautogui.press('enter') 

pyautogui.press('left', presses=3)
# 按下键
pyautogui.keyDown('ctrl')
# 释放键
pyautogui.keyUp('esc')

pyautogui.press(['left', 'left', 'left'])

# 摁下shift键,同时连续摁三下left
with pyautogui.hold('shift'):
        pyautogui.press(['left', 'left', 'left'])

3、屏幕操作

3.1 屏幕截图

import pyautogui

# 截图整个屏幕,返回Image对象
image = pyautogui.screenshot()
# 截图整个屏幕,返回Image对象,并保存图片
image = pyautogui.screenshot('screenshot.png')
# 截取屏幕指定区域
image = pyautogui.screenshot(region(0,0,400,400))

3.2 定位

import pyautogui

# 获取图片在屏幕中的坐标,返回(100,100,400,400)
# Box(left,top,width,height)
# 可以使用location[0]、location.left 获取坐标值
# confidence定位图像的精度(可选)
# minSearchTime 最大搜索时间(一般情况下很快,毫秒级别)
# grayscale 灰度匹配,默认False,会大概提升30%
location = pyautogui.locateOnScreen('a.png',confidence=0.8,grayScale=True,minSearchTime=30)
# 可以将location传入center,获取中心坐标(x,y)
point = pyautogui.center(location)

# 返回图片的中心坐标x,y
x,y = pyautogui.locateCenterOnScreen('a.png',confidence=0.8)

# 返回一个生成器,生成图像在屏幕上的位置(left,top,wight,height)数组
locationes = pyautogui.locateAllOnScreen(image, grayscale=False)
# 返回needleImage在haystackImage的位置
location = pyautogui.locate(needleImage, haystackImage, grayscale=False)
# 返回一个位置数组
locationes = location.locateAll(needleImage, haystackImage, grayscale=False)

3.3 像素匹配

import pyautogui

# 获取屏幕截图中像素的RGB颜色(red=130,green=135,blue=144)
image = pyautogui.screenshot()
pix = image.getpixel((100,200))

# 获取指定坐标的RGB
pix = pyautogui.pixel(100,200)

# 判断指定坐标的RGB是否相同
# tolerance 可以变化的程度
boolean = pyautogui.pixelMatchesColor(100, 200, (130, 135, 144), tolerance=10)

4、其他功能

4.1 屏幕大小(分辨率)

screen_width,screen_height = pyautogui.size()

4.2 定位当前坐标

x,y = pyautogui.position()

# 判断坐标是否在屏幕上
boolean = pyautogui.onScreen(1920, 1080)

4.3 故障保护

# 启用故障保护功能,可以在程序运行时,将鼠标移动到(0,0)坐标来中断程序
pyautogui.FAILSAFE = True

4.4 延迟操作

# 每个Pyautogui执行后,都会延迟0.5秒,默认是0.1秒,可以设置为0
pyautogui.PAUSE = 0.5

4.5 消息框

4.5.1 alert

import pyautogui

# 显示消息框并获取返回值
result = pyautogui.alert("This is an alert box.", title="Alert Title")

# 检查返回值
if result == 'OK':
    print("User clicked OK.")

4.5.2 confirm

import pyautogui
# 显示一个带标题的确认对话框
# text: 对话框中显示的消息文本。
# title: 对话框的标题(可选)。
# buttons: 一个列表,包含对话框中按钮的文本。默认值为 ["OK", "Cancel"]。

response = pyautogui.confirm("Do you want to continue?", title="Confirmation", buttons=["Yes", "No"])

# 检查用户的选择
if response == "Yes":
    print("User chose to continue.")
else:
    print("User chose not to continue.")

4.5.3 prompt

import pyautogui

# 显示一个带标题的输入对话框
# text:对话框中显示的消息文本
# title:对话框的标题(可选)
# default:对话框中的默认文本(可选)
user_input = pyautogui.prompt("Please enter your name:", title="User Input")

# 检查用户输入
if user_input is not None:
    print(f"User entered: {user_input}")
else:
    print("User cancelled the input.")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值