python自动脚本的pyautogui入门学习

本文介绍了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', ‘:', ‘;', ‘<', ‘=', ‘>', ‘?', ‘@', ‘[', ‘\', ‘]', ‘^', ‘_', ‘`',
‘a', ‘b', ‘c', ‘d', ‘e',‘f', ‘g', ‘h', ‘i', ‘j', ‘k', ‘l', ‘m', ‘n', ‘o',
‘p', ‘q', ‘r', ‘s', ‘t', ‘u', ‘v', ‘w', ‘x', ‘y', ‘z', ‘{', ‘|', ‘}', ‘~',
‘accept', ‘add', ‘alt', ‘altleft', ‘altright', ‘apps', ‘backspace',
‘browserback', ‘browserfavorites', ‘browserforward', ‘browserhome',
‘browserrefresh', ‘browsersearch', ‘browserstop', ‘capslock', ‘clear',
‘convert', ‘ctrl', ‘ctrlleft', ‘ctrlright', ‘decimal', ‘del', ‘delete',
‘divide', ‘down', ‘end', ‘enter', ‘esc', ‘escape', ‘execute', ‘f1', ‘f10',
‘f11', ‘f12', ‘f13', ‘f14', ‘f15', ‘f16', ‘f17', ‘f18', ‘f19', ‘f2', ‘f20',
‘f21', ‘f22', ‘f23', ‘f24', ‘f3', ‘f4', ‘f5', ‘f6', ‘f7', ‘f8', ‘f9',
‘final', ‘fn', ‘hanguel', ‘hangul', ‘hanja', ‘help', ‘home', ‘insert', ‘junja',
‘kana', ‘kanji', ‘launchapp1', ‘launchapp2', ‘launchmail',
‘launchmediaselect', ‘left', ‘modechange', ‘multiply', ‘nexttrack',
‘nonconvert', ‘num0', ‘num1', ‘num2', ‘num3', ‘num4', ‘num5', ‘num6',
‘num7', ‘num8', ‘num9', ‘numlock', ‘pagedown', ‘pageup', ‘pause', ‘pgdn',
‘pgup', ‘playpause', ‘prevtrack', ‘print', ‘printscreen', ‘prntscrn',
‘prtsc', ‘prtscr', ‘return', ‘right', ‘scrolllock', ‘select', ‘separator',
‘shift', ‘shiftleft', ‘shiftright', ‘sleep', ‘space', ‘stop', ‘subtract', ‘tab',
‘up', ‘volumedown', ‘volumemute', ‘volumeup', ‘win', ‘winleft', ‘winright', ‘yen',
‘command', ‘option', ‘optionleft', ‘optionright']

消息盒子

python自动脚本的pyautogui入门学习_第1张图片

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

返回button值

python自动脚本的pyautogui入门学习_第2张图片

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

返回输入值

python自动脚本的pyautogui入门学习_第3张图片

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

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

python自动脚本的pyautogui入门学习_第4张图片

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入门学习_第5张图片

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入门学习_第6张图片

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

到此这篇关于python自动脚本的pyautogui入门学习的文章就介绍到这了,更多相关pyautogui入门 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(python自动脚本的pyautogui入门学习)