0

Iam currently trying to automate a installing process, which I am doing with pywinautogui. I would like to check if a checkbox was checked (which is actually a button), because in some cases the checkbox is alreay checked and it would uncheck it if i pressed it again. is there any way i can figure the state?

app = Application().Start(cmd_line=u'installer.exe')
windowsformswindowappbarad = app[u'Installer']
windowsformswindowappbarad.wait('ready')
windowsformsbuttonappbarad = windowsformswindowappbarad.Button4
#print(checkbox.get_click_state())
windowsformsbuttonappbarad.click()

this is how the Checkbox looks like

Edit:

app = Application().Start(cmd_line=u'installer.exe')
windowsformswindowappbarad = app[u'Installer']
windowsformswindowappbarad.set_focus()
windowsformswindowappbarad.wait('ready')
checkbox_object = windowsformswindowappbarad.Button4()

if checkbox_object.get_toggle_state() == 0:
    print("not set")
elif checkbox_object.get_toggle_state() == 1:
    print("ein")

2 Answers2

1

With the below code, you can get the Check box state.

Checkbox_object = window_object.wrapper_object()
 if checkbox_object.get_toggle_state() == 0:
            return False
 elif checkbox_object.get_toggle_state() == 1:
            return True

Let me know if you face issue .

Below is the code I tried :

from pywinauto.application import Application

app1 = Application(backend='uia').connect(title="H4)")
app1.H4.set_focus()
checkboxobject=app1.Dialog.CheckBox0
value=checkboxobject.get_toggle_state()
print(value)

If value is 0 Check box is not checked , if 1 it is checked

Sreevathsabr
  • 649
  • 7
  • 23
  • I tried this, iam realy confused right now because if use the object of the window i get AttributeError: 'DialogWrapper' object has no attribute 'get_toggle_state' if i use the object of the button i get the same Attribute error but with 'ButtenWrapper' – HugosBanane Mar 17 '21 at 10:15
  • Can you post the code which you tried with this . – Sreevathsabr Mar 17 '21 at 10:42
  • I believe `Button4()` is the alias name of the UI object . So can you remove the `()` in code and try . – Sreevathsabr Mar 17 '21 at 11:06
  • that worked for me, idk why but Application(backend='uia') solved the problem, thanks – HugosBanane Mar 17 '21 at 11:55
  • 1
    @HugosBanane please go through https://pywinauto.readthedocs.io/en/latest/getting_started.html this . – Sreevathsabr Mar 17 '21 at 12:57
0

Sometimes the get_toggle_state(), get_check_state() or is_checked() methods do not work. In these cases the only way is to use:

wrapper.legacy_properties()['State']

You can retrieve the value of the states with inspect.exe in Legacy|Accessible.State

David Pratmarty
  • 596
  • 1
  • 4
  • 19
  • checkbox = ButtonWrapper(windowsformsbuttonappbarad.wrapper_object()) checkbox.legacy_properties()['State'] i am not sure how to use it becaucause the ButtonWrapper doesnt have that attribute, could you please explain? – HugosBanane Mar 17 '21 at 09:38
  • Did you try to print the value of windowsformsbuttonappbarad.wrapper_object().legacy_properties()['State'] when your checkbox is checked and then not checked? – David Pratmarty Mar 17 '21 at 09:59
  • yeah but i get this error message AttributeError: 'ButtonWrapper' object has no attribute 'legacy_properties' – HugosBanane Mar 17 '21 at 10:13
  • Can you see a Legacy|Accessible.State with inspect.exe ? – David Pratmarty Mar 17 '21 at 10:16