Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 246
Igor Markovic Avatar asked Oct 31 '25 04:10

Igor Markovic


1 Answers

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

like image 55
ncica Avatar answered Nov 01 '25 19:11

ncica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!