import psutil from pywinauto.application import Application from pywinauto import Desktop, WindowSpecification class ConnApp: @staticmethod def conn_single_proc_app(proc_name: str, proc_path: str, app_backend: str = "win32", wait_time: int | float | None = None, retry_freq: int | float | None = None) -> Application: """ 连接单进程应用 :param proc_name: 进程名 :param proc_path: 进程路径 :param app_backend: 应用后端 :param wait_time: 等待时间 :param retry_freq: 重试频率 :return: 应用 """ # 获取进程id proc_id = None for proc in psutil.process_iter(): if proc.name() == proc_name: proc_id = proc.pid break app = Application(backend=app_backend) # 连接应用 if proc_id is None: app.start(cmd_line=proc_path, timeout=wait_time, retry_interval=retry_freq) else: app.connect(process=proc_id, timeout=wait_time, retry_interval=retry_freq) return app @staticmethod def conn_multi_proc_app(dlg_title: str, app_backend: str = "win32") -> WindowSpecification: """ 连接多进程应用 :param dlg_title: 对话框标题 :param app_backend: 应用后端 :return: 对话框 """ desktop = Desktop(backend=app_backend) # 获取对话框 dlg = desktop.window(title=dlg_title) return dlg
标签:None,retry,封装,app,param,proc,连接,backend From: https://www.cnblogs.com/monkeyDragon/p/17500052.html