Igor Markovic
Igor Markovic

Reputation: 185

grabbing and setting windowsize in windows with python

I've searched all over, but I haven't found a way to either get the size of a certain window in windows, or setting the size after you have checked which size it is.

I'd be glad if someone could help me out where to search. I'm working in windows 10 with python and opencv as well.

I've found Selenium WebDriver, but this seems to work with browsers only. I'd like to know if it's possible for other windows than browsers, Notepad for example.

In sum, I'd like to get the size of a certain window and changing it if it's not the correct size I want it to be. Thanks for thinking with me.

Upvotes: 4

Views: 4958

Answers (1)

ncica
ncica

Reputation: 7206

Module win32gui

Python extensions for Microsoft Windows’ Provides access to much of the Win32 API, the ability to create and use COM objects, and the Pythonwin environment


MoveWindow function: Changes the position and dimensions of the specified window.

syntax: MoveWindow(hwnd, x, y, width, height, bRepaint) **

import win32gui
#hwnd = win32gui.FindWindow(None, 'Window Title')
hwnd = win32gui.FindWindow(None, 'Untitled - Notepad')

x0, y0, x1, y1 = win32gui.GetWindowRect(hwnd)
w = x1 - x0 # width
h = y1 - y0 # height

win32gui.MoveWindow(hwnd, x0, y0, w-500, h-500, True)

result:

  • App (full screen stretched): Notepad

enter image description here

  • Run code (tested pyCharm,python 3.7 project interpreter)

enter image description here

Upvotes: 8

Related Questions