Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get window position & size with python

Tags:

python

windows

How can I get and set the window (any windows program) position and size with python?

like image 725
Bruno 'Shady' Avatar asked Aug 22 '11 01:08

Bruno 'Shady'


People also ask

How to get window size and position?

1] Use Shift key while closing a window However, you need to click on that same button when holding the Shift key on your keyboard. This trick helps Windows OS to remember the window position.

How do I get the Windows position in Python?

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 .

How do you adjust a window position?

Right-click the window tab and select Move>View (to move a separate window). Move the mouse to where you want the window to display and left-click to confirm the move.


1 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() 
like image 172
icktoofay Avatar answered Sep 17 '22 11:09

icktoofay