Rohan Mahy
Rohan Mahy

Reputation: 105

How do I check/uncheck items in a ListBox with pywinauto?

I am trying to automate the Cambium LINKPlanner application using pywinauto, and found controls for almost everything I needed. However, there is a checked list box which has the identifier 'ListBox' and there doesn't seem to be any way to check/uncheck the items in the list.

Screen shot of (checked) ListBox

I can list the contents of the ListBox:

>>> app.LINKPlanner.ListBox.item_texts()
['PMP 450b High Gain', 'PMP 450b Mid-gain', 'PMP450 (retired)', 'PMP450d (retired)', 'PMP450i', 'PMP450i ATEX/HAZLOC']

I can highlight an item in the ListBox either by name or position:

>>> app.LINKPlanner.ListBox.select('PMP450i')
<win32_control.ListBoxWrapper - '', ListBox, 70154>
>>> app.LINKPlanner.ListBox.select(2)
<win32_control.ListBoxWrapper - '', ListBox, 70154>

But I can't check/uncheck the checkbox inside the list items.

Any suggestions?

EDIT:

Using Vasily's suggestion, I was able to see the bounding box, then use click_input to click the checkbox.

>>> app.LINKPlanner.ListBox.select('PMP 450b High Gain').item_rect(0)
<RECT L0, T0, R276, B17>
>>> app.LINKPlanner.ListBox.select('PMP 450b High Gain').click_input(coords=(9,9))

Upvotes: 2

Views: 1475

Answers (2)

florisla
florisla

Reputation: 13528

I'm using .select("item text") and .send_keys("{SPACE}").

I tried Vasily's answer to use item_rect(). That works but it has some drawbacks.

In my case item_rect was not reliable: it was always 2 to 4 pixels taller than in reality. This error adds up for consecutive items. My script ended up clicking the wrong checkboxes.

Also, if the list is too long it requires scrolling. This requires a separate workaround.

I found my ListBox would toggle the checkbox at the press of the space bar.

So I'm doing this instead:

# De-select all (there is a button to do this).
dialog.window(title="Deselect all").click()

# Select the desired items.
for item in ["Item 1 name", "Item 5 name"]:
   dialog.ListBox.select(item)
   dialog.ListBox.type_keys("{SPACE}")

Upvotes: 0

Vasily Ryabov
Vasily Ryabov

Reputation: 9991

If it's detected as ListBox, it may be owner-drawn check boxes which can't be detected separately. I'd suggest using method .item_rect(item_name) and then method .click_input(). Also method .client_to_screen() may be helpful.

Upvotes: 3

Related Questions