中文 | English
API: UIA Menu
The Menu control organizes commands and options. It is commonly seen in:
- Application top menu bars (File, Edit, View, ...)
- Right-click context menus
- Dropdown menus
Finding a menu
Note: Menus usually have names (such as "文件" or "编辑") and can be found by name.
python
from wingman import uia
# 查找名为"文件"的菜单
menu = uia.find_by_name("文件")
if menu:
print("找到菜单")1
2
3
4
5
6
2
3
4
5
6
Expanding a menu and selecting
Expand the menu and click a menu item
Note: First expand the menu, then find and click the target menu item.
Steps:
- Find the menu control
- Use
expand()to expand the menu - Wait for the menu items to appear
- Find and click the target menu item
python
from wingman import uia, util
# 查找并展开"文件"菜单
menu = uia.find_by_name("文件")
if menu:
# 展开菜单
menu["expand"]()
print("已展开文件菜单")
# 等待菜单项出现
util.sleep(300)
# 查找并点击"新建"菜单项
new_item = uia.find_by_name("新建")
if new_item:
new_item["click"]()
print("已点击新建")1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Clicking a menu item directly
Note: Some applications support clicking menu items directly without expanding the menu first.
python
from wingman import uia
# 某些应用支持直接点击菜单项
save_item = uia.find_by_name("保存")
if save_item:
save_item["click"]()
print("已点击保存")1
2
3
4
5
6
7
2
3
4
5
6
7
Right-click menu (context menu)
Note: The right-click menu is a context menu triggered by the right mouse button.
Steps:
- Right-click the target location
- Wait for the menu to appear
- Find and click a menu item
python
from wingman import uia, input, util
# 在指定位置右键(第 3 参 button=2 为右键)
input.click(100, 100, 2)
print("已右键")
# 等待上下文菜单出现
util.sleep(300)
# 操作菜单项
copy_item = uia.find_by_name("复制")
if copy_item:
copy_item["click"]()
print("已点击复制")1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
Getting the menu item list
Note: After expanding the menu, you can iterate over all menu items.
python
from wingman import uia, util
menu = uia.find_by_name("文件")
if menu:
# 展开菜单
menu["expand"]()
util.sleep(300)
# 获取所有菜单项
items = menu["get_children"]()
print(f"文件菜单共有 {len(items)} 个项目:")
for item in items:
info = item["get_info"]()
name = info.get('name', '(无名称)')
print(f" - {name}")1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Available Interfaces
| Python method | Lua method | Description |
|---|---|---|
expand() | :expand() | Expand the menu |
collapse() | :collapse() | Collapse the menu |
get_children() | :get_children() | Get all menu items |
click() | :click() | Click a menu item |