2

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?

Shi Jie Tio
  • 2,479
  • 5
  • 24
  • 41

2 Answers2

2

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?

darksky
  • 1,955
  • 16
  • 28
  • May I know how to make unactive window stay active? because once I run on the script, the python console become active window and it will go maximize the python window instead of the window I open in the script – Shi Jie Tio Mar 01 '19 at 00:05
  • Like I said, you'd need to find the HWND of the window that you want to maximize. If you open a new window from inside your script, you have to find the HWND of that new window. – darksky Mar 02 '19 at 21:50
1

This approach worked for me, I combined it with another code to find my window, and worked just fine, thank you. I am looking for a window named "outlook", bring it forward then maximize.

 # maximize window
top_windows = []
win32gui.EnumWindows(windowEnumerationHandler, top_windows)
for i in top_windows:
    if "outlook" in i[1].lower():
        print(i)
        hwnd = win32gui.GetForegroundWindow()
        win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
        break
cosminm
  • 11
  • 3