当前位置:   article > 正文

python autogui_【方法】pyautogui入门学习(python自动脚本)

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’]

消息盒子

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

返回button值

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

返回输入值

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

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

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])

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/121114
推荐阅读
相关标签
  

闽ICP备14008679号