
Pyautogui Automation
- 14 installs
- 9 repo stars
- Updated August 4, 2026
- steelan9199/wechat-publisher-skill
Helps with automation & workflows tasks.
About
pyautogui-automation is a Claude Code skill for automation & workflows. It helps solo builders move faster with AI-assisted development.
- pyautogui-automation
- Automation & Workflows
- AI-coding skill
Pyautogui Automation by the numbers
- 14 all-time installs (skills.sh)
- Ranked #1,417 of 2,715 Automation & Workflows skills by installs in the Skillselion catalog
- Data as of Aug 5, 2026 (Skillselion catalog sync)
npx skills add https://github.com/steelan9199/wechat-publisher-skill --skill pyautogui-automationAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 14 |
|---|---|
| repo stars | ★ 9 |
| Last updated | August 4, 2026 |
| Repository | steelan9199/wechat-publisher-skill ↗ |
What it does
Helps with automation & workflows tasks.
Files
PyAutoGUI 自动化操作
功能概览
| 功能类别 | 支持的操作 |
|---|---|
| 截图 | 全屏截图、区域截图、截图到剪贴板 |
| 鼠标控制 | 点击、双击、移动、相对移动、拖拽、滚动、按下/释放 |
| 颜色操作 | 获取像素颜色、查找颜色位置 |
| 键盘操作 | 输入文本、按键、组合键、快捷操作(复制/粘贴/全选等) |
| 图像识别 | 在屏幕上查找图片位置、等待图片出现/消失 |
| 对话框 | 警告、确认、输入对话框 |
| 系统信息 | 屏幕分辨率、鼠标位置、窗口信息 |
| 工具 | 等待、暂停 |
快速开始
基本使用模式
python scripts/automation.py <action> [参数...]所有操作返回 JSON 格式结果。
脚本位置: scripts/automation.py
操作详解
截图
# 全屏截图(自动生成文件名)
python scripts/automation.py screenshot
# 指定输出路径
python scripts/automation.py screenshot --output my_screenshot.png
# 区域截图
python scripts/automation.py screenshot --output region.png --region 100,100,400,300
# 截图到剪贴板(需要安装 pywin32)
python scripts/automation.py screenshot_to_clipboard
python scripts/automation.py screenshot_to_clipboard --region 100,100,400,300鼠标点击
# 左键单击坐标 (100, 200)
python scripts/automation.py click --x 100 --y 200
# 右键双击
python scripts/automation.py click --x 100 --y 200 --button right --clicks 2
# 快捷双击
python scripts/automation.py double_click --x 100 --y 200颜色操作
# 获取指定坐标的颜色
python scripts/automation.py get_pixel_color --x 100 --y 200
# 返回: {"rgb": [255, 255, 255], "hex": "#ffffff"}
# 查找颜色位置(精确匹配)
python scripts/automation.py find_color --rgb 255,255,255
# 查找颜色(带容差)
python scripts/automation.py find_color --rgb 255,255,255 --tolerance 10
# 在指定区域查找
python scripts/automation.py find_color --rgb 255,0,0 --region 0,0,800,600鼠标控制
# 获取当前鼠标位置
python scripts/automation.py get_mouse_position
# 移动鼠标(瞬间)
python scripts/automation.py move_mouse --x 500 --y 300
# 移动鼠标(动画效果,0.5秒)
python scripts/automation.py move_mouse --x 500 --y 300 --duration 0.5
# 相对当前位置移动鼠标
python scripts/automation.py move_mouse_rel --x 100 --y -50
python scripts/automation.py move_mouse_rel --x 100 --y -50 --duration 0.5
# 拖拽鼠标
python scripts/automation.py drag_mouse --x 800 --y 600 --duration 1.0
# 鼠标按下(不释放)
python scripts/automation.py mouse_down --button left
# 鼠标释放
python scripts/automation.py mouse_up --button left
# 滚动(正数向上,负数向下)
python scripts/automation.py scroll --amount 500
python scripts/automation.py scroll --amount -500 --x 500 --y 300屏幕信息
# 获取屏幕分辨率
python scripts/automation.py get_screen_size
# 返回: {"width": 1920, "height": 1080}
# 获取当前活动窗口信息(需要安装 pywin32)
python scripts/automation.py get_active_window
# 返回: {"title": "窗口标题", "left": 100, "top": 100, "width": 800, "height": 600}
# 获取所有可见窗口列表(需要安装 pywin32)
python scripts/automation.py get_all_windows
# 返回: {"count": 5, "windows": [...]}等待
# 等待 2 秒
python scripts/automation.py sleep --seconds 2键盘操作
# 输入文本
python scripts/automation.py type_text --text "Hello World"
# 输入文本(带间隔)
python scripts/automation.py type_text --text "Hello" --interval 0.1
# 按下按键
python scripts/automation.py press_key --key enter
python scripts/automation.py press_key --key esc
# 组合键
python scripts/automation.py hotkey --keys ctrl,c
python scripts/automation.py hotkey --keys ctrl,shift,esc
# 快捷操作
python scripts/automation.py copy # Ctrl+C
python scripts/automation.py paste # Ctrl+V
python scripts/automation.py cut # Ctrl+X
python scripts/automation.py select_all # Ctrl+A
python scripts/automation.py undo # Ctrl+Z
python scripts/automation.py redo # Ctrl+Y
python scripts/automation.py save # Ctrl+S常用按键名称: enter, esc, tab, space, backspace, delete, up, down, left, right, f1-f12, ctrl, alt, shift, win
图像识别
需要安装 opencv-python 以使用 confidence 参数:
# 查找图片位置
python scripts/automation.py locate_on_screen --image button.png
# 使用置信度(需要 opencv-python)
python scripts/automation.py locate_on_screen --image button.png --confidence 0.9
# 在指定区域查找
python scripts/automation.py locate_on_screen --image button.png --region 0,0,800,600
# 查找所有匹配位置
python scripts/automation.py locate_all_on_screen --image icon.png
# 等待图片出现(最多等待10秒)
python scripts/automation.py wait_for_image --image button.png --timeout 10
# 等待图片出现(自定义检查间隔)
python scripts/automation.py wait_for_image --image loading.png --timeout 30 --wait_interval 1
# 等待图片消失
python scripts/automation.py wait_for_image_to_vanish --image loading.png --timeout 30对话框
# 警告对话框
python scripts/automation.py alert --title "提示" --text "操作完成"
# 确认对话框
python scripts/automation.py confirm --title "确认" --text "是否继续?"
# 自定义按钮
python scripts/automation.py confirm --title "选择" --text "请选择操作" --buttons "保存,不保存,取消"
# 输入对话框
python scripts/automation.py prompt --title "输入" --text "请输入名称:" --default "默认值"安全设置
脚本已启用以下安全保护:
- FAILSAFE: 将鼠标快速移动到屏幕左上角会触发异常停止
- PAUSE: 每个操作后有 0.1 秒默认暂停
完整示例
自动化登录流程
# 1. 截图记录初始状态
python scripts/automation.py screenshot --output login_start.png
# 2. 点击用户名输入框
python scripts/automation.py click --x 500 --y 300
# 3. 输入用户名
python scripts/automation.py type_text --text "myusername"
# 4. 按 Tab 切换到密码框
python scripts/automation.py press_key --key tab
# 5. 输入密码
python scripts/automation.py type_text --text "mypassword"
# 6. 点击登录按钮
python scripts/automation.py click --x 500 --y 400
# 7. 等待页面加载
python scripts/automation.py sleep --seconds 3
# 8. 截图记录结果
python scripts/automation.py screenshot --output login_end.png颜色检测自动化
# 检测特定位置颜色并执行操作
color=$(python scripts/automation.py get_pixel_color --x 100 --y 100)
# 解析 JSON 判断颜色后执行相应操作图像定位点击
# 查找按钮并点击
result=$(python scripts/automation.py locate_on_screen --image submit_button.png --confidence 0.9)
# 从结果中提取 center_x, center_y 并点击依赖安装
脚本会自动安装必需的依赖:
pyautogui: 核心自动化库pillow: 图像处理
可选依赖(用于图像识别置信度):
pip install opencv-python资源索引
- automation.py - 主入口脚本
- utils.py - 公共工具函数
- mouse.py - 鼠标操作
- keyboard.py - 键盘操作
- screen.py - 截图和图像识别
- color.py - 颜色检测
- dialog.py - 对话框操作
- system.py - 系统信息
注意事项
1. 坐标系: 屏幕左上角为原点 (0, 0),向右为 X 增加,向下为 Y 增加 2. 权限: Windows 上可能需要以管理员权限运行某些操作 3. 分辨率: 多显示器环境下,坐标可能跨越多个屏幕 4. 图像识别: 受屏幕分辨率和缩放比例影响,建议使用 confidence 参数提高鲁棒性
快速参考表
| 用户输入 | AI 行动 |
|---|---|
| "xxx" | 直接执行 xxx |
#!/usr/bin/env python3
"""
PyAutoGUI 自动化操作脚本 - 主入口
支持截图、点击、颜色获取、鼠标控制等功能
输出格式: JSON
错误处理: 非零退出码 + stderr 输出错误信息
"""
import argparse
import json
import sys
# 先导入 utils 并确保依赖
from utils import ensure_dependencies, parse_region, parse_rgb
# 确保依赖已安装
ensure_dependencies()
# 导入各模块
import color
import dialog
import keyboard
import mouse
import screen
import system
# 定义所有支持的操作
ACTIONS = {
# 截图
"screenshot": lambda args: screen.screenshot(
args.output, parse_region(args.region)
),
"screenshot_to_clipboard": lambda args: screen.screenshot_to_clipboard(
parse_region(args.region)
),
# 鼠标
"click": lambda args: mouse.click(
args.x, args.y, args.button, args.clicks, args.interval
),
"double_click": lambda args: mouse.double_click(args.x, args.y, args.button),
"mouse_down": lambda args: mouse.mouse_down(args.button),
"mouse_up": lambda args: mouse.mouse_up(args.button),
"get_mouse_position": lambda args: mouse.get_mouse_position(),
"move_mouse": lambda args: mouse.move_mouse(args.x, args.y, args.duration),
"move_mouse_rel": lambda args: mouse.move_mouse_rel(args.x, args.y, args.duration),
"drag_mouse": lambda args: mouse.drag_mouse(
args.x, args.y, args.duration, args.button
),
"scroll": lambda args: mouse.scroll(args.amount, args.x, args.y),
# 颜色
"get_pixel_color": lambda args: color.get_pixel_color(args.x, args.y),
"find_color": lambda args: color.find_color(
parse_rgb(args.rgb), parse_region(args.region), args.tolerance
),
# 系统
"get_screen_size": lambda args: system.get_screen_size(),
"get_active_window": lambda args: system.get_active_window(),
"get_all_windows": lambda args: system.get_all_windows(),
"sleep": lambda args: system.sleep(args.seconds),
# 键盘
"type_text": lambda args: keyboard.type_text(args.text, args.interval),
"press_key": lambda args: keyboard.press_key(args.key),
"hotkey": lambda args: keyboard.hotkey(*args.keys.split(",")),
"copy": lambda args: keyboard.copy(),
"paste": lambda args: keyboard.paste(),
"cut": lambda args: keyboard.cut(),
"select_all": lambda args: keyboard.select_all(),
"undo": lambda args: keyboard.undo(),
"redo": lambda args: keyboard.redo(),
"save": lambda args: keyboard.save(),
# 图像识别
"locate_on_screen": lambda args: screen.locate_on_screen(
args.image, args.confidence, parse_region(args.region)
),
"locate_all_on_screen": lambda args: screen.locate_all_on_screen(
args.image, args.confidence, parse_region(args.region)
),
"wait_for_image": lambda args: screen.wait_for_image(
args.image,
args.confidence,
parse_region(args.region),
args.timeout,
args.wait_interval,
),
"wait_for_image_to_vanish": lambda args: screen.wait_for_image_to_vanish(
args.image,
args.confidence,
parse_region(args.region),
args.timeout,
args.wait_interval,
),
# 对话框
"alert": lambda args: dialog.alert(args.title, args.text, args.button or "OK"),
"confirm": lambda args: dialog.confirm(
args.title, args.text, args.buttons.split(",") if args.buttons else None
),
"prompt": lambda args: dialog.prompt(args.title, args.text, args.default or ""),
}
REQUIRED_PARAMS = {
"click": ["x", "y"],
"double_click": ["x", "y"],
"get_pixel_color": ["x", "y"],
"move_mouse": ["x", "y"],
"move_mouse_rel": ["x", "y"],
"drag_mouse": ["x", "y"],
"scroll": ["amount"],
"find_color": ["rgb"],
"sleep": ["seconds"],
"type_text": ["text"],
"press_key": ["key"],
"hotkey": ["keys"],
"locate_on_screen": ["image"],
"locate_all_on_screen": ["image"],
"wait_for_image": ["image"],
"wait_for_image_to_vanish": ["image"],
}
def main():
parser = argparse.ArgumentParser(description="PyAutoGUI 自动化操作")
parser.add_argument("action", choices=list(ACTIONS.keys()), help="要执行的操作")
# 通用参数
parser.add_argument("--x", type=int, help="X 坐标")
parser.add_argument("--y", type=int, help="Y 坐标")
parser.add_argument("--duration", type=float, default=0.0, help="持续时间")
parser.add_argument("--output", help="输出文件路径")
parser.add_argument(
"--button", default="left", choices=["left", "right", "middle"], help="鼠标按钮"
)
parser.add_argument("--clicks", type=int, default=1, help="点击次数")
parser.add_argument("--interval", type=float, default=0.0, help="点击间隔")
parser.add_argument("--rgb", help="目标颜色 RGB,格式: R,G,B")
parser.add_argument("--tolerance", type=int, default=0, help="颜色容差")
parser.add_argument("--region", help="搜索区域,格式: x,y,w,h")
parser.add_argument("--text", help="要输入的文本")
parser.add_argument("--key", help="按键名称")
parser.add_argument("--keys", help="组合键,用逗号分隔")
parser.add_argument("--image", help="图像文件路径")
parser.add_argument(
"--confidence", type=float, help="置信度 (0-1),需要 opencv-python"
)
parser.add_argument("--title", default="提示", help="对话框标题")
parser.add_argument("--buttons", help="按钮列表,用逗号分隔")
parser.add_argument("--default", default="", help="默认值")
parser.add_argument("--amount", type=int, help="滚动量")
parser.add_argument("--seconds", type=float, help="等待秒数")
parser.add_argument("--timeout", type=float, default=10, help="等待超时时间(秒)")
parser.add_argument(
"--wait_interval", type=float, default=0.5, help="等待检查间隔(秒)"
)
args = parser.parse_args()
# 检查必需参数
if args.action in REQUIRED_PARAMS:
missing = [p for p in REQUIRED_PARAMS[args.action] if getattr(args, p) is None]
if missing:
print(
f"错误: 操作 '{args.action}' 缺少必需参数: {', '.join(missing)}",
file=sys.stderr,
)
sys.exit(1)
ensure_dependencies()
try:
result = ACTIONS[args.action](args)
print(json.dumps(result, ensure_ascii=False, indent=2))
if not result.get("success", False):
sys.exit(1)
except Exception as e:
print(
json.dumps({"success": False, "error": str(e)}, ensure_ascii=False),
file=sys.stderr,
)
sys.exit(1)
if __name__ == "__main__":
main()
#!/usr/bin/env python3
"""
颜色操作模块
"""
from utils import pyautogui
def get_pixel_color(x, y):
"""获取指定坐标的颜色 (RGB)"""
color = pyautogui.pixel(x, y)
return {
"success": True,
"x": x,
"y": y,
"rgb": color,
"hex": "#{:02x}{:02x}{:02x}".format(*color),
}
def find_color(target_rgb, region=None, tolerance=0):
"""在屏幕上查找指定颜色"""
if region is None:
width, height = pyautogui.size()
region = (0, 0, width, height)
x, y, w, h = region
screenshot = pyautogui.screenshot(region=region)
positions = []
target_r, target_g, target_b = target_rgb
for px in range(w):
for py in range(h):
r, g, b = screenshot.getpixel((px, py))
if (
abs(r - target_r) <= tolerance
and abs(g - target_g) <= tolerance
and abs(b - target_b) <= tolerance
):
positions.append({"x": x + px, "y": y + py})
return {
"success": True,
"target_rgb": target_rgb,
"found_count": len(positions),
"positions": positions[:50],
}
#!/usr/bin/env python3
"""
对话框操作模块
"""
from utils import pyautogui
def alert(title, text, button="OK"):
"""显示警告对话框"""
pyautogui.alert(text=text, title=title, button=button)
return {"success": True}
def confirm(title, text, buttons=None):
"""显示确认对话框"""
if buttons is None:
buttons = ["OK", "Cancel"]
result = pyautogui.confirm(text=text, title=title, buttons=buttons)
return {"success": True, "result": result}
def prompt(title, text, default=""):
"""显示输入对话框"""
result = pyautogui.prompt(text=text, title=title, default=default)
return {"success": True, "result": result}
#!/usr/bin/env python3
"""
键盘操作模块
"""
from utils import pyautogui
def type_text(text, interval=0.0):
"""输入文本"""
pyautogui.typewrite(text, interval=interval)
return {"success": True, "text": text, "interval": interval}
def press_key(key):
"""按下键盘按键"""
pyautogui.press(key)
return {"success": True, "key": key}
def hotkey(*keys):
"""按下组合键"""
pyautogui.hotkey(*keys)
return {"success": True, "keys": list(keys)}
def copy():
"""复制 (Ctrl+C)"""
pyautogui.hotkey("ctrl", "c")
return {"success": True, "action": "copy"}
def paste():
"""粘贴 (Ctrl+V)"""
pyautogui.hotkey("ctrl", "v")
return {"success": True, "action": "paste"}
def cut():
"""剪切 (Ctrl+X)"""
pyautogui.hotkey("ctrl", "x")
return {"success": True, "action": "cut"}
def select_all():
"""全选 (Ctrl+A)"""
pyautogui.hotkey("ctrl", "a")
return {"success": True, "action": "select_all"}
def undo():
"""撤销 (Ctrl+Z)"""
pyautogui.hotkey("ctrl", "z")
return {"success": True, "action": "undo"}
def redo():
"""重做 (Ctrl+Y)"""
pyautogui.hotkey("ctrl", "y")
return {"success": True, "action": "redo"}
def save():
"""保存 (Ctrl+S)"""
pyautogui.hotkey("ctrl", "s")
return {"success": True, "action": "save"}
#!/usr/bin/env python3
"""
鼠标操作模块
"""
from utils import pyautogui
def click(x, y, button="left", clicks=1, interval=0.0):
"""在指定坐标点击"""
pyautogui.click(x, y, button=button, clicks=clicks, interval=interval)
return {"success": True, "x": x, "y": y, "button": button, "clicks": clicks}
def double_click(x, y, button="left"):
"""在指定坐标双击"""
pyautogui.doubleClick(x, y, button=button)
return {"success": True, "x": x, "y": y, "button": button}
def get_mouse_position():
"""获取当前鼠标位置"""
x, y = pyautogui.position()
return {"success": True, "x": x, "y": y}
def move_mouse(x, y, duration=0.0):
"""移动鼠标到指定位置"""
pyautogui.moveTo(x, y, duration=duration)
return {"success": True, "x": x, "y": y, "duration": duration}
def move_mouse_rel(x, y, duration=0.0):
"""相对当前位置移动鼠标"""
pyautogui.moveRel(x, y, duration=duration)
return {"success": True, "x": x, "y": y, "duration": duration}
def drag_mouse(x, y, duration=0.0, button="left"):
"""拖拽鼠标到指定位置"""
pyautogui.dragTo(x, y, duration=duration, button=button)
return {"success": True, "x": x, "y": y, "duration": duration, "button": button}
def mouse_down(button="left"):
"""按下鼠标按钮(不释放)"""
pyautogui.mouseDown(button=button)
return {"success": True, "button": button}
def mouse_up(button="left"):
"""释放鼠标按钮"""
pyautogui.mouseUp(button=button)
return {"success": True, "button": button}
def scroll(amount, x=None, y=None):
"""滚动鼠标滚轮"""
if x is not None and y is not None:
pyautogui.scroll(amount, x, y)
else:
pyautogui.scroll(amount)
return {"success": True, "amount": amount, "x": x, "y": y}
#!/usr/bin/env python3
"""
屏幕操作模块 - 截图和图像识别
"""
import io
import time
from pathlib import Path
from utils import pyautogui
def screenshot(output_path=None, region=None):
"""截图并保存到文件"""
if output_path is None:
timestamp = time.strftime("%Y%m%d_%H%M%S")
output_path = f"screenshot_{timestamp}.png"
img = pyautogui.screenshot(region=region)
img.save(output_path)
return {"success": True, "path": str(Path(output_path).resolve())}
def screenshot_to_clipboard(region=None):
"""截图并复制到剪贴板"""
try:
import win32clipboard
img = pyautogui.screenshot(region=region)
output = io.BytesIO()
img.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:]
output.close()
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
win32clipboard.CloseClipboard()
return {"success": True, "message": "截图已复制到剪贴板"}
except ImportError:
return {"success": False, "error": "需要安装 pywin32: pip install pywin32"}
except Exception as e:
return {"success": False, "error": str(e)}
def locate_on_screen(image_path, confidence=None, region=None):
"""在屏幕上查找图片位置"""
try:
kwargs = {"region": region}
if confidence is not None:
kwargs["confidence"] = confidence
location = pyautogui.locateOnScreen(image_path, **kwargs)
if location:
center = pyautogui.center(location)
return {
"success": True,
"found": True,
"left": int(location.left),
"top": int(location.top),
"width": int(location.width),
"height": int(location.height),
"center_x": int(center.x),
"center_y": int(center.y),
}
return {"success": True, "found": False}
except pyautogui.ImageNotFoundException:
return {"success": False, "error": "未找到图像,请确保图像在屏幕上可见"}
except Exception as e:
return {"success": False, "error": f"错误: {type(e).__name__}: {str(e)}"}
def locate_all_on_screen(image_path, confidence=None, region=None):
"""在屏幕上查找所有匹配的图片位置"""
try:
kwargs = {"region": region}
if confidence is not None:
kwargs["confidence"] = confidence
locations = list(pyautogui.locateAllOnScreen(image_path, **kwargs))
results = []
for loc in locations:
center = pyautogui.center(loc)
results.append(
{
"left": int(loc.left),
"top": int(loc.top),
"width": int(loc.width),
"height": int(loc.height),
"center_x": int(center.x),
"center_y": int(center.y),
}
)
return {"success": True, "found_count": len(results), "locations": results}
except Exception as e:
return {"success": False, "error": str(e)}
def wait_for_image(image_path, confidence=None, region=None, timeout=10, interval=0.5):
"""等待图片出现在屏幕上"""
try:
start_time = time.time()
while time.time() - start_time < timeout:
kwargs = {"region": region}
if confidence is not None:
kwargs["confidence"] = confidence
location = pyautogui.locateOnScreen(image_path, **kwargs)
if location:
center = pyautogui.center(location)
return {
"success": True,
"found": True,
"waited": round(time.time() - start_time, 2),
"left": int(location.left),
"top": int(location.top),
"width": int(location.width),
"height": int(location.height),
"center_x": int(center.x),
"center_y": int(center.y),
}
time.sleep(interval)
return {
"success": True,
"found": False,
"waited": timeout,
"message": f"等待超时,{timeout}秒内未找到图片",
}
except Exception as e:
return {"success": False, "error": str(e)}
def wait_for_image_to_vanish(
image_path, confidence=None, region=None, timeout=10, interval=0.5
):
"""等待图片从屏幕上消失"""
try:
start_time = time.time()
while time.time() - start_time < timeout:
kwargs = {"region": region}
if confidence is not None:
kwargs["confidence"] = confidence
location = pyautogui.locateOnScreen(image_path, **kwargs)
if location is None:
return {
"success": True,
"vanished": True,
"waited": round(time.time() - start_time, 2),
}
time.sleep(interval)
return {
"success": True,
"vanished": False,
"waited": timeout,
"message": f"等待超时,{timeout}秒内图片仍未消失",
}
except Exception as e:
return {"success": False, "error": str(e)}
def get_screen_size():
"""获取屏幕分辨率"""
width, height = pyautogui.size()
return {"success": True, "width": width, "height": height}
#!/usr/bin/env python3
"""
系统信息模块
"""
import time
from utils import pyautogui
def get_screen_size():
"""获取屏幕分辨率"""
width, height = pyautogui.size()
return {"success": True, "width": width, "height": height}
def get_active_window():
"""获取当前活动窗口信息(Windows)"""
try:
import win32gui
hwnd = win32gui.GetForegroundWindow()
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
title = win32gui.GetWindowText(hwnd)
return {
"success": True,
"title": title,
"left": left,
"top": top,
"width": right - left,
"height": bottom - top,
"right": right,
"bottom": bottom,
}
except ImportError:
return {"success": False, "error": "需要安装 pywin32: pip install pywin32"}
except Exception as e:
return {"success": False, "error": str(e)}
def get_all_windows():
"""获取所有可见窗口列表(Windows)"""
try:
import win32gui
windows = []
def callback(hwnd, extra):
if win32gui.IsWindowVisible(hwnd):
title = win32gui.GetWindowText(hwnd)
if title:
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
windows.append(
{
"title": title,
"left": left,
"top": top,
"width": right - left,
"height": bottom - top,
}
)
win32gui.EnumWindows(callback, None)
return {"success": True, "count": len(windows), "windows": windows}
except ImportError:
return {"success": False, "error": "需要安装 pywin32: pip install pywin32"}
except Exception as e:
return {"success": False, "error": str(e)}
def sleep(seconds):
"""等待指定秒数"""
time.sleep(seconds)
return {"success": True, "slept": seconds}
#!/usr/bin/env python3
"""
公共工具函数
"""
import sys
def ensure_dependencies():
"""确保依赖已安装"""
packages = []
try:
import pyautogui as _pg
_pg # 使用变量避免未使用警告
except ImportError:
packages.append("pyautogui")
try:
from PIL import Image as _img
_img # 使用变量避免未使用警告
except ImportError:
packages.append("pillow")
if packages:
import subprocess
subprocess.check_call(
[sys.executable, "-m", "pip", "install"] + packages + ["-q"]
)
global pyautogui, Image
import pyautogui
from PIL import Image
pyautogui.FAILSAFE = True
pyautogui.PAUSE = 0.1
def parse_region(region_str):
"""解析区域字符串 x,y,w,h 为元组"""
return tuple(map(int, region_str.split(","))) if region_str else None
def parse_rgb(rgb_str):
"""解析 RGB 字符串 R,G,B 为元组"""
return tuple(map(int, rgb_str.split(",")))