Search code examples
pythonautomationui-automationpywinauto

How to add an item to a listbox with PyWinAuto?


I am using Python Version 3.8.2 (64bit) I am using PyWinAuto version 0.6.8

I am trying to automate a windows desktop app. I need to add items to a listbox.

so Far I can print the number of items in the list box. No problem

    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("**********************************")

does anyone out there know how I would add an item to the listBox?


Solution

  • The default backend is "win32". But fortunately it's correct backend to try adding an element to the list box. You can try .send_message(LB_ADDSTRING, wParam, 0). wParam for this message can be found in the Microsoft docs: https://learn.microsoft.com/en-us/windows/win32/controls/lb-addstring

    You will need ctypes.create_unicode_buffer(...) or ctypes.create_string_buffer(...) for wParam depending on the application is Unicode or not.

    Example of similar call to send_message can be found in def set_edit_text implementation in win32_controls.py in pywinauto source code. It's not so complicated.

    Hope it helps.