Search code examples
pythonpython-3.6pywin32win32gui

How to maximize an inactive window?


I have multiple window, but i wish to maximize one of the window only, below is my scripts:

import win32gui, win32con
win32gui.ShowWindow('C:/Desktop/UD.ca', win32con.SW_MAXIMIZE)

After run this script i get bellow error:

Error

TypeError: The object is not a PyHANDLE object

Anyone have idea on this?


Solution

  • You need the HWND of the window that you want to maximize. 'C:/Desktop/UD.ca' is not an HWND. Think of it as a unique ID for a window.

    As an example, you can get the HWND of the foreground window by

    hwnd = win32gui.GetForegroundWindow()
    

    and then pass that in the call to ShowWindow,

    win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
    

    If you want to search all visible Windows for one that contains a title, see Get HWND of each Window?