脚本专栏 
首页 > 脚本专栏 > 浏览文章

python自动脚本的pyautogui入门学习

(编辑:jimmy 日期: 2024/5/20 浏览:3 次 )

本文介绍了pyautogui入门学习,分享给大家,也给自己留个笔记

安装

pip install pyautogui

学习使用

加载模块

import pyautogui

获取信息类

电脑屏幕的左上角是位置(0,0),向右X坐标增加,向下Y坐标增加

# 获取屏幕尺寸
screenWidth, screenHeight = pyautogui.size()

# 获取鼠标当前位置
currentMouseX, currentMouseY = pyautogui.position()

进行操作类

鼠标

# 鼠标移动到屏幕中心
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)
# 将鼠标移动到固定位置
pyautogui.moveTo(100, 100)
# 用“1秒”的时间移动到固定位置
pyautogui.moveTo(100, 100, duration=1)
# 将鼠标移动到相对当前“下方100”的位置
pyautogui.moveRel(0, 100) 


# 鼠标左键拖拽到屏幕“100,200”的位置
pyautogui.dragTo(100, 200, button='left') 
# 鼠标左键用“2秒”拖拽到屏幕“300,400”的位置
pyautogui.dragTo(300, 400, 2, button='left') 
# 鼠标左键用“2秒”拖拽到相对当前“右边30”的位置
pyautogui.dragRel(30, 0, 2, button='left')
### button属性可以有:left\middle\right


# 鼠标左键单击
pyautogui.click()
# 鼠标左键单击“100,200”位置
pyautogui.click(x=100, y=200)
# 鼠标左键双击
pyautogui.doubleClick()
# 鼠标左键三击
pyautogui.tripleClick()
# 鼠标右键单击
pyautogui.click(button='right')
# 鼠标右键单击
pyautogui.rightClick()
# 鼠标右键间隔0.25秒单击3次
pyautogui.click(button='right', clicks=3, interval=0.25)


# 鼠标左键放下
pyautogui.mouseDown()
# 鼠标左键抬起
pyautogui.mouseUp()


# 鼠标滚轮上滚10
pyautogui.scroll(10)
# 鼠标滚轮下滚10
pyautogui.scroll(-10)

键盘

# 输入一个字符串
pyautogui.typewrite('Hello world!')
# 每个字母间隔0.25秒输入一个字符串
pyautogui.typewrite('Hello world!', interval=0.25) 
# 键入 "a", 然后敲击左方向键, 再键入 "b".
pyautogui.typewrite(['a','left','b'])


# 按键
pyautogui.press('esc')
# 连续按键
pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])


# 键盘按下
pyautogui.keyDown('shift')
# 键盘释放
pyautogui.keyUp('shift')

# 热键按住ctrl的同时按下c
pyautogui.hotkey('ctrl', 'c')

支持的按键有:

['\t', ‘\n', ‘\r', ' ‘, ‘!', ‘"', ‘#', ‘$', ‘%', ‘&', "'", ‘(',
‘)', ‘*', ‘+', ‘,', ‘-', ‘.', ‘/', ‘0', ‘1', ‘2', ‘3', ‘4', ‘5', ‘6', ‘7',
‘8', ‘9', ‘:', ‘;', ‘<', ‘=', ‘>', ‘"color: #ff0000">消息盒子

python自动脚本的pyautogui入门学习

pyautogui.alert(text='你好吗', title='问候', button='我很好')

返回button值

python自动脚本的pyautogui入门学习

pyautogui.confirm(text='你好吗', title='问候', buttons=['我很好', '我不好', '不告诉你'])

返回输入值

python自动脚本的pyautogui入门学习

pyautogui.prompt(text='你好吗', title='问候' , default='')

将输入值用*号隐藏,返回输入值

python自动脚本的pyautogui入门学习

pyautogui.password(text='', title='', default='', mask='*')

截图功能

首先需要安装Pillow模块

im1 = pyautogui.screenshot()
# 截图整个屏幕并命名保存到本地
im2 = pyautogui.screenshot('my_screenshot.png')
# 截图区域“左侧,顶部,宽度和高度”
im = pyautogui.screenshot(region=(0, 0, 300, 400))


# 返回值(left, top, width, height)
button7location = pyautogui.locateOnScreen('calc7key.png')

实战练习

1、计算机自动计算

打开电脑自带的计算器
利用截图工具截取4张图,并依次命名
将python程序和计算器一同打开在桌面上
运行python程序

# 自动计算器输入
import pyautogui

x = [0]*4
x[0] = pyautogui.locateCenterOnScreen('7.png')
x[1] = pyautogui.locateCenterOnScreen('+.png')
x[2] = pyautogui.locateCenterOnScreen('5.png')
x[3] = pyautogui.locateCenterOnScreen('=.png')
for i in range(4):
  pyautogui.click(x[i])

python自动脚本的pyautogui入门学习

2、自动画图

# 自动画图
import pyautogui
import time

time.sleep(5)

distance = 200
while distance > 0:
  pyautogui.dragRel(distance, 0, duration=0.5) # move right
  distance -= 5
  pyautogui.dragRel(0, distance, duration=0.5) # move down
  pyautogui.dragRel(-distance, 0, duration=0.5) # move left
  distance -= 5
  pyautogui.dragRel(0, -distance, duration=0.5) # move up

python自动脚本的pyautogui入门学习

参见官网:https://pyautogui.readthedocs.io/en/latest/index.html

上一篇:Python气泡提示与标签的实现
下一篇:django 多数据库及分库实现方式
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网