PyAutoGUI 是一个用于自动化图形用户界面 (GUI) 的 Python 库。它允许你通过编写脚本来控制鼠标和键盘,从而实现对桌面应用程序的自动化操作。以下是对 PyAutoGUI 库的一些详细讲解,包括其主要功能和一些示例代码。
安装 PyAutoGUI
首先,你需要安装 PyAutoGUI 库。可以使用以下命令进行安装:
pip install pyautogui
主要功能
1. 鼠标控制
- 移动鼠标
- 点击鼠标
- 拖动鼠标
- 获取鼠标位置
2. 键盘控制
- 按键
- 输入文本
- 组合按键
3. 屏幕图像识别
- 截屏
- 查找屏幕上的图像
- 等待屏幕上的图像出现
4. 消息框
- 提示框
- 确认框
- 输入框
使用示例
1. 鼠标控制
- 移动鼠标
import pyautogui
pyautogui.moveTo(100, 100, duration=1) # 将鼠标移动到屏幕坐标 (100, 100),移动时间为 1 秒
- 点击鼠标
pyautogui.click(100, 100) # 在屏幕坐标 (100, 100) 处点击
- 拖动鼠标
pyautogui.dragTo(200, 200, duration=1) # 将鼠标拖动到屏幕坐标 (200, 200),拖动时间为 1 秒
- 获取鼠标位置
x, y = pyautogui.position()
print(f'Mouse position: ({x}, {y})')
2. 键盘控制
- 按键
pyautogui.press('enter') # 按下 Enter 键
- 输入文本
pyautogui.write('Hello, world!', interval=0.1) # 输入文本,每个字符之间间隔 0.1 秒
- 组合按键
pyautogui.hotkey('ctrl', 'c') # 按下 Ctrl+C 组合键
3. 屏幕图像识别
- 截屏
screenshot = pyautogui.screenshot()
screenshot.save('screenshot.png') # 保存截屏为 screenshot.png
- 查找屏幕上的图像
button_location = pyautogui.locateOnScreen('button.png')
print(button_location) # 输出按钮图像在屏幕上的位置
- 等待屏幕上的图像出现
button_location = pyautogui.locateOnScreen('button.png', timeout=10)
if button_location:
print('Button found on screen.')
else:
print('Button not found within 10 seconds.')
4. 消息框
- 提示框
pyautogui.alert('This is an alert box.')
- 确认框
response = pyautogui.confirm('Do you want to continue?')
print(response) # 输出 'OK' 或 'Cancel'
输入框
response = pyautogui.prompt('Please enter your name:')
print(response) # 输出用户输入的名字
注意事项
- 屏幕分辨率:确保你的脚本适用于你的屏幕分辨率。如果分辨率变化,鼠标坐标和图像识别可能会失效。
- 图像识别速度:图像识别可能会比较慢,尤其是在屏幕内容复杂的情况下。
- 异常处理:在编写自动化脚本时,建议添加适当的异常处理,以应对可能出现的错误和意外情况。
进阶功能
- 滚动鼠标
pyautogui.scroll(-500) # 向下滚动 500 个单位
- 拖动鼠标(相对位置)
pyautogui.dragRel(100, 0, duration=1) # 从当前位置向右拖动 100 像素
- 鼠标按钮操作
pyautogui.rightClick(100, 100) # 在屏幕坐标 (100, 100) 处右击
pyautogui.middleClick(100, 100) # 在屏幕坐标 (100, 100) 处中键点击
- 键盘组合键
pyautogui.hotkey('ctrl', 's') # 按下 Ctrl+S 组合键
PyAutoGUI 是一个强大的工具,可以帮助你实现桌面应用程序的自动化操作。通过这些功能,你可以编写脚本来模拟用户操作,从而完成重复性的任务,提高工作效率。
使用案例
1. 调用一个桌面程序以及使用pyautogui模拟手动点击程序
import os
import sys
import subprocess
import time
import pyautogui
def search_file(filename, search_paths):
for path in search_paths:
for root, dirs, files in os.walk(path):
if filename in files:
return os.path.join(root, filename)
return None
def find_application_path(app_name):
try:
result = subprocess.check_output(['where', app_name], shell=True)
print(result)
paths = result.decode().strip().split('\r\n')
if paths:
return paths[0]
except subprocess.CalledProcessError:
print(f"Error finding application path")
return None
class T1Automation:
def __init__(self, t1_path, app_path, channel_num):
self.t1_path = t1_path
self.app_path = app_path
self.channel_num = channel_num
def start_application(self):
subprocess.Popen([self.app_path])
time.sleep(30)
def open_project(self):
pyautogui.hotkey('alt', 'f')
pyautogui.hotkey('p')
pyautogui.hotkey('o')
pyautogui.write(self.t1_path)
pyautogui.press('enter')
time.sleep(5)
def set_view(self):
pyautogui.hotkey('alt', 'v')
for _ in range(13):
pyautogui.press('down')
pyautogui.press('enter')
time.sleep(5)
def find_image_with_retries(self, image_path, retries=5, delay=2):
for _ in range(retries):
location = pyautogui.locateOnScreen(image_path, confidence=0.8, grayscale=True)
if location:
return location
time.sleep(delay)
raise pyautogui.ImageNotFoundException(f"Could not locate the image: {image_path}")
def interact_with_interface(self):
dropdown_button = self.find_image_with_retries(r'.\Image\interface_hw.png')
if dropdown_button:
center = pyautogui.center(dropdown_button)
image_width = dropdown_button[2]
right_position = center[0] + image_width, center[1]
pyautogui.click(right_position)
time.sleep(5)
hw_name = self.get_hw_name()
pyautogui.typewrite(hw_name)
pyautogui.press('enter')
time.sleep(2)
else:
print("Could not locate the dropdown button")
def get_hw_name(self):
if self.channel_num == 1:
return "LGX1"
elif self.channel_num == 3:
return "Channel 3"
elif self.channel_num == 4:
return "Channel 4"
else:
return "others hw"
def apply_and_link(self):
apply_button = self.find_image_with_retries(r'.\Image\apply.png')
if apply_button:
apply_center = pyautogui.center(apply_button)
pyautogui.click(apply_center)
time.sleep(5)
link_button = self.find_image_with_retries(r'.\Image\link.png')
if link_button:
link_center = pyautogui.center(link_button)
image_height = link_button[3]
bottom_center = link_center[0], link_center[1] + image_height // 3
pyautogui.click(bottom_center)
else:
print("Could not locate the link button")
def download(self):
time.sleep(100)
download_button = self.find_image_with_retries(r'.\Image\download.png')
if download_button:
download_center = pyautogui.center(download_button)
_, _, width, height = download_button
half_height = height // 2
half_center = download_center[0], download_center[1] + half_height // 2
pyautogui.click(half_center)
else:
print("Could not locate the download button")
def close_application(self):
subprocess.call(['taskkill', '/F', '/IM', 'T1.exe'])
def run(self):
try:
self.start_application()
self.open_project()
self.set_view()
self.interact_with_interface()
self.apply_and_link()
self.download()
finally:
self.close_application()
if __name__ == "__main__":
filename = "T1.exe"
search_paths = ["C:\\", "D:\\", "E:\\"]
# if len(sys.argv) != 3:
# print("Usage: python script.py <t1p_project> <channel_num>")
# sys.exit(1)
# t1p_path = sys.argv[1]
# channel_num = int(sys.argv[2])
t1p_path = r"C:\Downloads\T1_project.t1p"
channel_num = 1
app_path = find_application_path(filename)
if not app_path:
app_path = search_file(filename, search_paths)
if app_path:
print(f"Found {filename} at {app_path}")
automation = T1Automation(t1p_path, app_path, channel_num)
automation.run()
else:
print(f"{filename} not found on C, D, or E drive. Please install {filename}.")
2. 实时获取鼠标位置坐标
import time
import subprocess
import pyautogui
if __name__ == "__main__":
while True:
previous_postion = pyautogui.position()
while pyautogui.position() == previous_position:
pass
current_mouse_x, current_mouse_y = pyautogui.position()
print(current_mouse_x, current_mouse_y)