Это было довольно сложно, но это должно сделать работу (она выполняет все шаги, которые вы перечислили - нажимает ОК - и закрывает программу):
import pywinauto
pywinauto.Application().start(r'mmc gpedit.msc')
app = pywinauto.Application(backend="uia").connect(path='mmc.exe')
admin_template = app.LocalGroupPolicyEditor.child_window(title="User Configuration", control_type="TreeItem").child_window(title="Administrative Templates", control_type="TreeItem")
admin_template.double_click_input()
start_menu = admin_template.child_window(title="Start Menu and Taskbar", control_type="TreeItem")
start_menu.double_click_input()
option_list = app.LocalGroupPolicyEditor.child_window(auto_id="12786", control_type="List")
# Just select any of the first options to change the focus to the list.
first_elem = option_list.child_window(title="Add Search Internet link to Start Menu", control_type="ListItem")
first_elem.click_input()
# Used to scroll down the window so that the wanted option becomes visible.
pywinauto.keyboard.send_keys("cccc")
option = option_list.child_window(title="Change Start Menu power button", control_type="ListItem")
option.double_click_input()
pop_up = app.LocalGroupPolicyEditor.child_window(auto_id="tableLayoutFullForm", control_type="Pane")
radio = pop_up.child_window(title="Enabled", auto_id="radioButtonEnabled", control_type="RadioButton")
radio.click_input()
drop_down = pop_up.child_window(title="Choose one of the following actions", auto_id="dropDownListChoose one of the following actions", control_type="ComboBox")
drop_down.click_input()
# 'Hack' to first select the Restart option and then the next option after that which starts with l (=Log off).
# This ensures that the correct setting gets set despite of what the setting was before.
pywinauto.keyboard.send_keys("rl{ENTER}")
ok = pop_up.child_window(title="OK", auto_id="buttonOK", control_type="Button")
ok.click_input()
app.kill()
Убедитесь, что этот скрипт запущен от имени администратора, иначе он потерпит неудачу.
Не стесняйтесь спрашивать, есть ли у вас какие-либо вопросы по поводу кода:)
Edit:
Если вы используете версию pywinauto <0.6.0, вам придется заменить два вхождения (строки 19 и 34) <code>pywinauto.keyboard.send_keys() на:
pywinauto.SendKeysCtypes.SendKeys()
Если это не сработает, вы можете попробовать:
pywinauto.keyboard.SendKeys()