3 启动程序
3.1 使用Application对象的start()方法
The timeout parameter is optional, it should only be necessary to
use if the application takes a long time to start up。
1)Win32
API (backend="win32") - a default backend for now
界面框架为MFC, VB6, VCL, simple WinForms controls
and most of the old legacy apps
实例:
from pywinauto.application import Application
app = Application().start("D:\TeleECG\TeleECGU.exe")
2)MS
UI Automation (backend="uia")
界面框架为WinForms, WPF, Store apps, Qt5,
browsers
Notes: Chrome requires --force-renderer-accessibility cmd flag
before starting. Custom properties and controls are not supported
because of comtypes Python library restrictions.
实例:
from
pywinauto.application import Application
app =
Application(backend="uia").start("D:\TeleECG\TeleECGU.exe")
4 连接到已有的进程
使用Application对象的connect()方法。这个方法对已有进程的绑定非常灵活。
1)使用进程ID
(PID)进行绑定。
app =
Application().connect(process=19188)
进程的PID可以在任务管理器中查看。
2)使用窗口句柄绑定
app =
Application().connect(handle=0x00230DB6)
窗口句柄可以在Spy++中查看
3)使用程序路径绑定
app =
Application().connect(path=r"D:\Program
Files (x86)\app.exe")
4)使用标题、类型等匹配
app =
Application().connect(title_re="标题",
class_name="TMainForm")
第1、2种方法通用性不强,每次运行ID和窗口句柄都可能不一样。第3种方法最直接简单,而第4种方法灵活性最强。
5 定义为通用函数
win.py
# -*- coding: utf-8 -*-frompywinauto.application importApplication
defstart_app(path):
"""启动程序。界面框架为MFC, VB6, VCL, simple WinForms controls and most of the old legacy apps.:parampath:软件安装路径里的运行程序。:return:Return appeg.:app = start_app(r"F:/TeleECG/TeleECGU.exe")"""app = Application(backend="win32").start(path)
returnapp
defconnect_app(path):
"""连接已打开的程序。界面框架为MFC, VB6, VCL, simple WinForms controls and most of the old legacy apps.:parampath:软件路径。:return:Return app"""app = Application().connect(path=path)
returnapp
defstart_uia_app(path):
"""启动程序。界面框架为WinForms, WPF, Store apps, Qt5, browsers:parampath:软件路径。:return:Return appPywinauto中backend有两种:win32和uia,默认为win32。可使用spy++和Inspect工具判断backend适合写哪种。例如:如果使用Inspect的UIA模式,可见的控件和属性更多的话,backend可选uia,反之,backend可选win32。"""app = Application(backend="uia").start(path)
returnapp