简体   繁体   中英

How can I press “Start” windows button via python and win32api/pywinauto?

I need to hide "Start menu" window via python if it's open. Clicking on coordinates does not work for me because script will be running on Windows 11. Also, it would be nice if I could check if "Start menu" is visible etc.

UPD: Code that works now.

desktop = Desktop(backend='uia')
start_menu = desktop.window(title='Start', control_type='Window')
if start_menu.exists():
    pywinauto.keyboard.send_keys('{LWIN down}{LWIN up}')

If there is nothing special in Win11 (I haven't tried it yet), this is how it works on Win10:

from pywinauto import Desktop

taskbar = Desktop(backend="uia").window(title="Taskbar", control_type="Pane")
# taskbar.dump_tree() # print the subtree like it should be coded with pywinauto

# we will check if "Start" window exists later (it's kind of locator)
start_window = Desktop(backend="uia").window(title="Start", control_type="Window")

# wait up to 2 seconds until window exists or return False
# if window already exists, it returns True almost immediately
if start_window.exists(timeout=2):
    # hide the menu
    taskbar.child_window(title="Start", control_type="Button").click()
import pyautogui

pyautogui.hotkey('winleft')

Source Keyboard Keys

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM