如何使用 PyWinAuto 将项目添加到列表框?

How to add an item to a listbox with PyWinAuto?

我正在使用 Python 版本 3.8.2(64 位) 我正在使用 PyWinAuto 版本 0.6.8

我正在尝试自动化 windows 桌面应用程序。我需要将项目添加到列表框。

到目前为止,我可以打印列表框中的项目数。没问题

    app = Application()
app.connect(path=r"C:\Program Files (x86)\Example\WindowsFormsApp.exe")

#Get a dialog...
dlg = app.top_window()
dlg.print_control_identifiers()

#Click the 'Generate File' buton'
windowHandle = app.window(best_match='Example Windows Forms App')
listBox = windowHandle.ListBox
print("**********************************") 
print(listBox.item_count())
print("**********************************")

有人知道我如何将项目添加到列表框吗?

默认后端是 "win32"。但幸运的是,尝试向列表框添加元素是正确的后端。你可以试试.send_message(LB_ADDSTRING, wParam, 0)wParam 此消息可在 Microsoft 文档中找到:https://docs.microsoft.com/en-us/windows/win32/controls/lb-addstring

wParam 需要 ctypes.create_unicode_buffer(...)ctypes.create_string_buffer(...),具体取决于应用程序是否为 Unicode。

可以在 pywinauto 源代码的 win32_controls.py 中的 def set_edit_text 实现中找到对 send_message 的类似调用示例。没那么复杂。

希望对您有所帮助。