How can I get and set the window (any windows program) position and size with python?
-
A window owned by the current process, or any top-level window shown on the desktop? – André Caron Aug 22 '11 at 01:27
-
1I'd like to know if there is any way to do this without having to install third party software or non-native libraries. It seems logically that it should be possible. If not though, then why not?. – Musixauce3000 Apr 27 '16 at 18:45
7 Answers
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()
-
-
2
-
2@seler: As my answer said, you can try one of the interfaces to X11. Personally, I'd use [xpyb](http://xcb.freedesktop.org/XcbPythonBinding/). Iterate through the windows, finding the ones you want, and then get their geometries. The geometries contain the position and size of the windows. – icktoofay May 16 '12 at 03:26
-
1for a linux solution, see http://stackoverflow.com/questions/1380784/how-to-get-list-opened-windows-in-pygtk-or-gtk-in-ubuntu – DiCaprio Jan 22 '16 at 16:02
-
does not work for me: open windows calculator, make it to full screen on full hd monitor and getting strange value: Window Calculator: Location: (-7, -7) Size: (1550, 838). It has strict disproportion for 1.25 rate and stable shift for -7, -7 for top left point. looks really strange – user2602807 Apr 05 '21 at 23:31
-
@user2602807 any idea on how to fix these issues? I also face the same – FoundABetterName Dec 25 '21 at 07:21
-
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
.

- 951,095
- 183
- 1,149
- 1,285
As Greg Hewgill mentioned, if you know the name of the window, you can simply use win32gui's FindWindow, and GetWindowRect. This is perhaps a little cleaner and efficient than previous methods.
from win32gui import FindWindow, GetWindowRect
# FindWindow takes the Window Class name (can be None if unknown), and the window's display text.
window_handle = FindWindow(None, "Diablo II")
window_rect = GetWindowRect(window_handle)
print(window_rect)
#(0, 0, 800, 600)
For future reference: PyWin32GUI has now moved to Github
-
I'm getting `(-32000, -32000, -31840, -31972)` when the window is fullscreen (e.g. Diablo / Overwatch) – David Jul 16 '21 at 16:33
this can return window rect from window title
Code
def GetWindowRectFromName(name:str)-> tuple:
hwnd = ctypes.windll.user32.FindWindowW(0, name)
rect = ctypes.wintypes.RECT()
ctypes.windll.user32.GetWindowRect(hwnd, ctypes.pointer(rect))
# print(hwnd)
# print(rect)
return (rect.left, rect.top, rect.right, rect.bottom)
if __name__ == "__main__":
print(GetWindowRectFromName('CALC'))
pass
Environment
Python 3.8.2 | packaged by conda-forge | (default, Apr 24 2020, 07:34:03) [MSC v.1916 64 bit (AMD64)] on win32 Windows 10 Pro 1909

- 181
- 1
- 11
-
if you using windows, `ctypes.windll.user32` and `ctypes.wintypes` are loaded on default. – ShortArrow Jan 14 '22 at 05:53
For Linux you can use the tool I made here. The tool was meant for a slightly different use but you can use the API directly for your needs.
Install tool
sudo apt-get install xdotool xprop xwininfo
git clone https://github.com/Pithikos/winlaunch.git && cd winlaunch
In terminal
>>> from winlaunch import *
>>> wid, pid = launch('firefox')
>>> win_pos(wid)
[3210, 726]
wid
and pid
stand for window id and process id respectively.

- 18,827
- 15
- 113
- 136
Something not mentioned in any of the other responses is that in newer Windows (Vista and up), "the Window Rect now includes the area occupied by the drop shadow.", which is what win32gui.GetWindowRect
and ctypes.windll.user32.GetWindowRect
are interfacing with.
If you want to get the positions and sizes without the dropshadow, you can:
- Manually remove them. In my case there were 10 pixels on the left, bottom and right which had to be pruned.
- Use the
dwmapi
to extract theDWMWA_EXTENDED_FRAME_BOUNDS
as mentioned in the article
On using the dwmapi.DwmGetWindowAttribute
(see here):
This function takes four arguments: The hwnd, the identifier for the attribute we are interested in, a pointer for the data structure in which to write the attribute, the size of this data structure. The identifier we get by checking this enum. In our case, the attribute DWMWA_EXTENDED_FRAME_BOUNDS
is on position 9.
import ctypes
from ctypes.wintypes import HWND, DWORD, RECT
dwmapi = ctypes.WinDLL("dwmapi")
hwnd = 133116 # refer to the other answers on how to find the hwnd of your window
rect = RECT()
DMWA_EXTENDED_FRAME_BOUNDS = 9
dwmapi.DwmGetWindowAttribute(HWND(hwnd), DWORD(DMWA_EXTENDED_FRAME_BOUNDS),
ctypes.byref(rect), ctypes.sizeof(rect))
print(rect.left, rect.top, rect.right, rect.bottom)
Lastly: "Note that unlike the Window Rect, the DWM Extended Frame Bounds are not adjusted for DPI".

- 21
- 1