需求:我购买的 DIgSILENT PowerFactory 软件不能从外部api运行python脚本,只能从软件中用本地的python核去运行脚本(需要在软件内手动点开,不能直接外部api控制启动)
为此,我需要
- 自动控制,重启软件或打开软件
- 模拟“CTRL+D"打开数据资源管理器
- 模拟鼠标点击命令运行窗口
- 模拟键盘输入自动化脚本命令
- 监控鼠标状态,监控 DIgSILENT PowerFactory 中自动化脚本是否完成
- 在自动化脚本运行完成后给出提示
具体的:
- 模拟光标位置
# 移动鼠标到指定位置 pyautogui.moveTo(x, y)
- 模拟鼠标点击
# 执行鼠标左键点击 pyautogui.click(button='left')
- 模拟键盘切换中英问输入法
# 尝试切换到英文输入法(根据您的系统设置可能需要调整按键) pyautogui.hotkey('shift') # 或者 'ctrl', 'shift'
- 模拟键盘输入
# 添加代码,键盘输入"ashbravo\CloseOn"并回车 pyautogui.write('ashbravo\CloseOn\n') # 注意反斜杠需要转义
完整代码如下,运行后,给出UI界面,运行脚本,可以控制 键盘快捷键、键盘输入、鼠标点击
import ctypes
import tkinter as tk
from threading import Thread
import pyautogui
import subprocess
import time
from tkinter import messagebox
""" 当前适用分辨率 2560*1600 """
# 添加全局变量用于更新UI
status_label_text = None
# Windows API 调用来获取光标信息
class CURSORINFO(ctypes.Structure):
_fields_ = [("cbSize", ctypes.c_int),
("flags", ctypes.c_int),
("hCursor", ctypes.c_void_p),
("ptScreenPos", ctypes.c_long * 2)]
def get_cursor_info():
cursor_info = CURSORINFO()
cursor_info.cbSize = ctypes.sizeof(CURSORINFO)
ctypes.windll.user32.GetCursorInfo(ctypes.byref(cursor_info))
return cursor_info.hCursor
def wait_for_normal_cursor():
print("等待 DIgSILENT PowerFactory 任务完成...")
while True:
hCursor = get_cursor_info()
print(f"当前鼠标句柄: {hCursor}") # 打印当前光标句柄
# 当鼠标句柄变为65539时,认为任务执行结束
if hCursor == 65539:
print("任务执行结束。")
break
time.sleep(5) # 每5秒检查一次
def run_script():
try:
# 尝试关闭已经打开的DIgSILENT PowerFactory软件
subprocess.run(["taskkill", "/f", "/im", digsilent_process_name], check=True)
print(f"{digsilent_process_name} 已关闭。")
except subprocess.CalledProcessError:
print(f"{digsilent_process_name} 没有运行或无法关闭。")
time.sleep(1) # 等待进程结束
# DIgSILENT PowerFactory 的路径
digsilent_path = r"DIgSILENT PowerFactory 的路径"
# 打开DIgSILENT PowerFactory软件
subprocess.Popen(digsilent_path)
print("正在打开 DIgSILENT PowerFactory。")
time.sleep(3) # 等待软件打开,可能需要更长的时间
# 模拟按下Ctrl+D打开数据管理器
pyautogui.hotkey('ctrl', 'd')
print("已打开数据管理器。")
# 等待数据管理器打开
time.sleep(1)
# 想要点击的屏幕位置坐标(请替换为实际坐标)
x = 1446 # X坐标
y = 460 # Y坐标
# 移动鼠标到指定位置
pyautogui.moveTo(x, y)
print("移动鼠标到指定位置。")
# 等待一段时间让界面响应
time.sleep(0.5)
# 执行鼠标左键点击
pyautogui.click(button='left')
print("已点击命令输入窗口。")
# 等待一段时间让命令行窗口激活
time.sleep(0.5)
# 尝试切换到英文输入法(根据您的系统设置可能需要调整按键)
pyautogui.hotkey('shift') # 或者 'ctrl', 'shift'
time.sleep(0.5)
# 添加代码,键盘输入"ashbravo\CloseOn"并回车
pyautogui.write('ashbravo\CloseOn\n') # 注意反斜杠需要转义
print("已输入命令并尝试执行。")
"""
当前鼠标句柄: 65543
当前鼠标句柄: 65543
当前鼠标句柄: 65539
当前鼠标句柄: 65539
当65543变成65539就是执行完了
"""
""" 关闭数据管理器界面 """
x_dataManager = 1886 # X坐标
y_dataManager = 420 # Y坐标
pyautogui.moveTo(x_dataManager, y_dataManager)
time.sleep(0.5)
pyautogui.click(button='left')
print("已点击命令输入窗口。")
# 等待光标从漏斗形变回正常
print("等待 DIgSILENT PowerFactory 任务完成...")
wait_for_normal_cursor()
print("DIgSILENT PowerFactory 脚本执行完毕。")
# 脚本执行完毕后在主线程中显示消息框
tk.messagebox.showinfo("通知", "DIgSILENT PowerFactory 脚本执行完毕。")
# DIgSILENT PowerFactory的路径
digsilent_path = r"DIgSILENT PowerFactory 的路径"
digsilent_process_name = "PowerFactory.exe"
# 以下是GUI部分的代码
def run_script_threaded():
thread = Thread(target=run_script)
thread.start()
# 创建主窗口
root = tk.Tk()
root.title("PowerFactory Automation")
root.geometry('300x100')
# 添加运行按钮
run_button = tk.Button(root, text="运行脚本", command=run_script_threaded)
run_button.pack(expand=True)
# 启动事件循环
root.mainloop()