You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用inspect.exe查看Windows应用Listbox项文本?及Python代码异常排查

问题:无法查看Windows应用Listbox项内容及Python获取失败
  • 使用inspect.exe检查Windows应用的Listbox时,项的文本、名称、文本值、模式值字段均为空,怀疑项文本被隐藏
  • 编写Python代码尝试获取Listbox项,但返回项数为0,实际该Listbox包含5个项,代码及运行结果如下:
import win32gui
import win32con 

# Find the handle of the main window 
app_Class = "TkTopLevel" 
window_handle = win32gui.FindWindow(app_Class, None) 

if window_handle == 0: 
    print("Cannot find handle from window") 
    exit() 

# Find the handle of the listbox within the main window 
control_class = "TkChild" 
listbox_handle = win32gui.FindWindowEx(window_handle, None, control_class, None) 

if listbox_handle == 0: 
    print("Cannot find handle from listbox in window") 
    exit() 

# Get the count of items in the listbox 
item_count = win32gui.SendMessage(listbox_handle, win32con.LB_GETCOUNT, 0, 0)
print("Count of items in listbox is:", item_count) 

# Iterate over each item in the listbox and print its text 
for index in range(item_count): 
    # Get the length of the item's text 
    item_text_length = win32gui.SendMessage(listbox_handle, win32con.LB_GETTEXTLEN, index, 0) 
    # Create a buffer to hold the item's text 
    item_text_buffer = win32gui.PyMakeBuffer(item_text_length + 1) 
    # Get the item's text and store it in the buffer 
    win32gui.SendMessage(listbox_handle, win32con.LB_GETTEXT, index, item_text_buffer) 
    # Convert the buffer to a string 
    item_text = item_text_buffer[:item_text_length] 
    # Print the item's text 
    print("Item text", index, ":", item_text) 

运行结果:Count of items in listbox is: 0


可能的原因及解决方法

1. 控件句柄识别错误

TkChild是Tkinter控件的通用类名,窗口内可能存在多个同类型控件,你当前获取的句柄并非目标Listbox的。可以通过以下方式修正:

  • 使用Spy++工具确认Listbox的具体类名或其他属性(如控件ID、位置);
  • 遍历所有子控件,通过LB_GETCOUNT消息筛选出正确的Listbox句柄,示例代码:
    import win32gui
    import win32con
    
    def find_target_listbox(parent_handle):
        target_handle = 0
        def enum_child_callback(handle, extra):
            nonlocal target_handle
            class_name = win32gui.GetClassName(handle)
            if class_name == "TkChild":
                # 发送LB_GETCOUNT测试是否为有效Listbox
                count = win32gui.SendMessage(handle, win32con.LB_GETCOUNT, 0, 0)
                if count > 0:
                    target_handle = handle
                    return False  # 找到后停止遍历
            return True
        win32gui.EnumChildWindows(parent_handle, enum_child_callback, None)
        return target_handle
    
    # 替换原代码中的listbox_handle获取逻辑
    listbox_handle = find_target_listbox(window_handle)
    

2. Tkinter控件的非标准特性

Tkinter的Listbox并非原生Win32控件,而是由Tkinter自行绘制,标准的LB_*消息可能无法正常交互。这种情况下可以:

  • 如果是自研应用,直接通过Tkinter代码获取Listbox内容;
  • 使用对Tkinter支持更好的UI自动化库(如pywinauto),示例代码:
    from pywinauto import Application
    
    # 连接目标应用
    app = Application(backend="win32").connect(class_name="TkTopLevel")
    # 定位Listbox控件
    listbox = app.window(class_name="TkTopLevel").child_window(class_name="TkChild", control_type="ListBox")
    # 获取项内容
    items = listbox.items()
    print(f"Count of items in listbox is: {len(items)}")
    for idx, item in enumerate(items):
        print(f"Item text {idx}: {item.text()}")
    

3. 权限不足

确保你的Python程序以足够权限运行,部分应用需要管理员权限才能访问其控件内部数据。


内容的提问来源于stack exchange,提问作者Jarallah Assalami

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 01:00:38