Get window position & size with python

Assuming you're on Windows, try using pywin32's win32gui module with its EnumWindows and GetWindowRect functions.

If you're using Mac OS X, you could try using appscript.

For Linux, you can try one of the many interfaces to X11.

Edit: Example for Windows (not tested):

import win32gui

def callback(hwnd, extra):
    rect = win32gui.GetWindowRect(hwnd)
    x = rect[0]
    y = rect[1]
    w = rect[2] - x
    h = rect[3] - y
    print("Window %s:" % win32gui.GetWindowText(hwnd))
    print("\tLocation: (%d, %d)" % (x, y))
    print("\t    Size: (%d, %d)" % (w, h))

def main():
    win32gui.EnumWindows(callback, None)

if __name__ == '__main__':
    main()

You can get the window coordinates using the GetWindowRect function. For this, you need a handle to the window, which you can get using FindWindow, assuming you know something about the window (such as its title).

To call Win32 API functions from Python, use pywin32.

Tags:

Python

Windows