简介

PyAutoGUI 是一个 Python 模块,它可以用来控制鼠标和键盘,实现自动化操作用户界面的功能。它适用于Windows、macOS和Linux操作系统。PyAutoGUI 可以用于自动化测试、数据录入、自动化演示等场景。

这对于重复性的任务特别有用,比如表格数据的录入、游戏的自动化控制或者日常办公自动化。

基本原理

PyAutoGUI 库通过操作系统的底层接口发送虚拟的鼠标和键盘命令,这些命令仿佛是由真实的用户输入的。Python 的跨平台特性让 PyAutoGUI 不仅限于 Windows,同样可以在 macOS 和 Linux 系统上运行。

安装 PyAutoGUI

开始之前,你需要确保 Python 已经被安装在你的系统上。接下来,使用 pip 命令即可安装 PyAutoGUI:

windows

pip install pyautogui

mac

pip3 install pyobjc-core
pip3 install pyobjc
pip3 install pyautogui

linux

pip3 install python3-xlib
pip3 install pyautogui

使用 Python PyAutoGUI

在使用 Python 中 PyAutoGUI 提供的所有强大函数之前,我们必须首先在脚本中导入模块。

import pyautogui as pag

我们将为模块pyautogui使用别名,我们在上面将其称为 pag

1. PyAutoGUI基本功能

在处理任何脚本之前,我们最好知道哪些组件执行什么样的任务。

话虽如此,在 Python pyautogui中提供了多种很好的方法来处理输入

# Gets the size of the primary monitor.
screenWidth, screenHeight = pag.size() 
 
# Gets the XY position of the mouse.
currentMouseX, currentMouseY = pag.position() 
 
# Move the mouse to XY coordinates.
pag.moveTo(100, 150)
 
# Allows the script to click with the mouse.
pag.click()
 
# Move the mouse to XY coordinates and click it.
pag.click(100, 200)
 
# Find where button.png appears on the screen and click it.
pag.click('button.png') 
 
# Double clicks the mouse.
pag.doubleClick()
 
# The writing functionality provided by PyAutoGUI imitates keyboard input
pag.write('Hello world!')
 
# Presses the Esc key.
pag.press('esc')
 
# The keyDown button causes the script to hold down on a specific key.
pag.keyDown('shift')
 
# You can pass a list of keys to press, which will be consecutively executed.
pag.press(['left', 'left', 'left', 'left'])
 
# Lets go of a certain key.
pag.keyUp('shift')
 
 # The hotkey() function allows for a selection of keys for hotkey usage.
pag.hotkey('ctrl', 'c')
 
# Make an alert box appear and pause the program until OK is clicked.
pag.alert('This is the message to display.')

另外需要注意的是,该模块还提供了在脚本中工作的关键字,这些关键字可以通过pyautogui.KEY_NAMES访问。

2. 在 Python 中使用 PyAutoGUI 实现简单自动化

我们可以创建一个简单的垃圾邮件自动化,使用一些 Python 和pyautogui模块在任何平台上连续发送消息。

让我们首先导入几个模块来使用所需的函数。

import pyautogui as pag
import time
 
from tkinter import Tk
from tkinter.filedialog import askopenfilename

现在,以下是制作垃圾邮件机器人的方法。

2.1. 提供输入方法。

我们可以通过手动输入消息来提供输入,但是,这甚至会破坏自动发送垃圾邮件的目的。

因此,让我们使用文件来解析文件并将内容写入平台。我们将使用 tkinter 模块来选择要从中读取的文件。

Tk().withdraw()
 
filename = askopenfilename()

2.2. 创建延迟调整垃圾邮件的速度。

我们还需要在每条消息之间创建一个延迟,以便平台能够一条一条地接受消息,而不是由于平台输入延迟而覆盖自身的单个消息。

timeDelay = int(input("If you want a delay, enter the number of seconds for the delay : ").split()[0])
 
if timeDelay < 1:
    timeDelay = 1
 
time.sleep(5)

2.3. 使用 PyAutoGUI 发送垃圾邮件!

现在,我们可以使用该模块从文件中读取每个单词,并写入平台。

f = open(filename, "r")
for word in f:
    time.sleep(timeDelay)
    pag.typewrite(word)
    pag.press("enter")

3. 在 Python 中完成 PyAutogui 的实现

现在我们已经完成了代码,您的最终代码应如下所示,

import pyautogui as pag
import time
from tkinter import Tk
from tkinter.filedialog import askopenfilename
 
Tk().withdraw()
filename = askopenfilename()
print(filename)
 
timeDelay = int(input("If you want a delay, enter the number of seconds for the delay : ").split()[0])
 
if timeDelay < 1:
    timeDelay = 1
 
time.sleep(5)
 
f = open(filename, "r")
for word in f:
    time.sleep(timeDelay)
    pag.typewrite(word)
    pag.press("enter")