How do I take out the focus or minimize a window with Python?

前端 未结 3 426
鱼传尺愫
鱼传尺愫 2020-12-09 07:01

I need to get focus to a specified window, and the only way I\'m seeing on my head, is minimizing all windows on front of it until I get the right one...

How can I d

3条回答
  •  情书的邮戳
    2020-12-09 07:48

    You'll need to enumerate through the windows and match the title of the window to get the one you want. The code below searches for a window with "firefox" in the title and sets the focus

    To minimize the window use the following line:

    def enumHandler(hwnd, lParam):
       if 'firefox' in win32gui.GetWindowText(hwnd):
          win32gui.ShowWindow(hwnd, win32con.SW_MINIMIZE)
    
    win32gui.EnumWindows(enumHandler, None)
    

    This works for Windows 10, Python3.5 32bit, pywin32‑223.


    I reported the above case, but an error occurred.

    Traceback (most recent call last):

    TypeError: The object is not a PyHANDLE object

提交回复
热议问题