【Pywinauto库】10.1 pywinauto.base_wrapper控件
概述
pywinauto.base_wrapper
是所有后端中所有包装器的基类,提供了GUI元素操作的基本接口和通用功能。
BaseMeta 类
概述
Wrapper对象的抽象元类。
静态方法
find_wrapper(element)
抽象静态方法,用于找到合适的包装器。
BaseWrapper 类
概述
元素的抽象包装类,所有其他包装类都继承自此基类。
类属性
can_be_label = False
: 指示元素是否可以作为标签friendlyclassname = None
: 友好类名has_title = True
: 是否有标题windowclasses = []
: 窗口类列表writable_props
: 可写入的属性列表
主要方法
capture_as_image(rect=None)
返回控件的PIL图像。
参数:
rect
: 指定捕获区域(默认为整个控件)
示例:
image = control.capture_as_image()
image.save("screenshot.png")
children(**kwargs)
将此元素的子元素作为列表返回。
返回: BaseWrapper(或子类)实例的列表
class_name()
返回元素的类名。
click_input(button='left', coords=(None, None), button_down=True, button_up=True, double=False, wheel_dist=0, use_log=True, pressed='', absolute=False, key_down=True, key_up=True)
单击指定的坐标。
参数:
button
: 鼠标按钮(‘left’, ‘right’, ‘middle’, ‘x’)coords
: 点击坐标(默认:控件中心)double
: 是否双击(默认:False)wheel_dist
: 鼠标滚轮移动距离
注意: 需要控件在屏幕上可见,执行更逼真的"点击"模拟。
client_to_screen(client_point)
将点从客户端坐标映射到屏幕坐标。
control_count()
返回此控件的子项数量。
control_id()
返回元素的ID。
descendants(**kwargs)
以列表的形式返回此元素的后代。
double_click_input(button='left', coords=(None, None))
双击指定的坐标。
drag_mouse_input(dst=(0, 0), src=None, button='left', pressed='', absolute=True)
点击src,拖动到dst然后放下。
参数:
dst
: 目标位置(坐标或包装器对象)src
: 源位置(坐标或包装器对象)button
: 拖拽时按住的鼠标按钮
draw_outline(colour='green', thickness=2, fill=win32defines.BS_NULL, rect=None)
在窗口周围绘制轮廓。
参数:
colour
: 轮廓颜色thickness
: 轮廓厚度fill
: 填充方式rect
: 绘制区域
element_info
只读属性,获取ElementInfo对象。
friendly_class_name()
返回控件的友好类名。
from_point(x, y)
在指定的屏幕坐标(x,y)处获取元素的包装对象。
get_properties()
将控件的属性作为字典返回。
is_child(parent)
如果此元素是’parent’的子元素,则返回True。
is_dialog()
如果控件是顶级窗口,则返回True。
is_enabled()
检查元素是否启用。
is_visible()
检查元素是否可见。
iter_children(**kwargs)
迭代此元素的子级,返回生成器。
iter_descendants(**kwargs)
迭代此元素的后代,返回生成器。
move_mouse_input(coords=(0, 0), pressed='', absolute=True)
移动鼠标。
parent()
返回此元素的父元素。
press_mouse_input(button='left', coords=(None, None), pressed='', absolute=True, key_down=True, key_up=True)
使用SendInput按下鼠标按钮。
process_id()
返回拥有此窗口的进程的ID。
rectangle()
返回元素的矩形区域。
release_mouse_input(button='left', coords=(None, None), pressed='', absolute=True, key_down=True, key_up=True)
释放鼠标按钮。
right_click_input(coords=(None, None))
右键单击指定的坐标。
root()
返回根元素包装器(桌面)。
set_focus()
将焦点设置为此元素。
texts()
返回此控件的每个项目的文本。
top_from_point(x, y)
在指定的屏幕坐标(x,y)处获取顶级元素的包装器对象。
top_level_parent()
返回此控件的顶级窗口。
type_keys(keys, pause=None, with_spaces=False, with_tabs=False, with_newlines=False, turn_off_numlock=True, set_foreground=True)
使用keyboard.send_keys键入元素的键。
verify_actionable()
验证元素是否可见并已启用。
verify_enabled()
验证该元素是否已启用。
verify_visible()
验证元素是否可见。
wait_for_idle()
后端特定函数,用于等待线程或窗口的空闲状态。
was_maximized()
在最小化之前指示窗口是否最大化。
wheel_mouse_input(coords=(None, None), wheel_dist=1, pressed='')
操作鼠标滚轮。
window_text()
返回元素的窗口文本。
异常类
ElementNotEnabled
未启用元素时引发。
ElementNotVisible
元素不可见时引发。
InvalidElement
传递无效元素时引发。
工具函数
remove_non_alphanumeric_symbols(s)
使文本可用于属性名称,移除非字母数字符号。
使用示例
基本操作示例
from pywinauto import Application# 启动应用程序
app = Application().start("notepad.exe")# 获取记事本窗口
notepad = app.Notepad# 检查属性
print(f"类名: {notepad.class_name()}")
print(f"是否可见: {notepad.is_visible()}")
print(f"是否启用: {notepad.is_enabled()}")# 点击操作
notepad.click_input()# 截图
image = notepad.capture_as_image()
image.save("notepad.png")# 获取所有子控件
children = notepad.children()
for child in children:print(f"子控件: {child.friendly_class_name()}")# 关闭应用程序
notepad.close()
验证和异常处理
from pywinauto.base_wrapper import ElementNotEnabled, ElementNotVisibletry:control.verify_actionable() # 验证可操作性control.click_input() # 执行操作
except ElementNotEnabled:print("控件未启用,无法操作")
except ElementNotVisible:print("控件不可见,无法操作")
坐标转换
# 客户端坐标转屏幕坐标
client_point = (10, 20)
screen_point = control.client_to_screen(client_point)
print(f"屏幕坐标: {screen_point}")# 获取控件矩形
rect = control.rectangle()
print(f"控件位置: {rect.left}, {rect.top}")
print(f"控件大小: {rect.width()} x {rect.height()}")
继承关系
所有具体的控件包装类(如ButtonWrapper、EditWrapper等)都继承自BaseWrapper,并重写或扩展了相应的方法来实现特定控件的功能。