Oct-18-2020, 05:14 PM
Getting info about the container window is a little outside pygame's scope. But that doesn't mean it's not possible, you just have to reach into other, more platform-specific avenues.
On Windows, you can get the info using ctypes:
On Windows, you can get the info using ctypes:
import pygame as pg import ctypes from ctypes import wintypes def build_win_info_function(): # api docs: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowrect # describe the windows api function we want # GetWindowRect returns a bool (success/fail), a window handler (HWND), # and a pointer to a rect (which the ctypes module will return to us) builder = ctypes.WINFUNCTYPE( wintypes.BOOL, wintypes.HWND, ctypes.POINTER(wintypes.RECT) ) # 1==we're passing this in, 2==ctypes is passing it back to us flags = ((1, "hwnd"), (2, "lprect")) # now that we've described the function, we indicate where it exists so we can call it func = builder(("GetWindowRect", ctypes.windll.user32), flags) return func def main(window): # pygame tracks the window handler, and makes it available window_handler = pg.display.get_wm_info()["window"] # build a GetWindowRect get_window_rect = build_win_info_function() position = {"top": 0, "left": 0, "right": 0, "bottom": 0} last = None while True: # pump events, so the window can move for ev in pg.event.get(): # close window event if ev.type == pg.QUIT: return # get the current window info window_info = get_window_rect(window_handler) position["top"] = window_info.top position["left"] = window_info.left position["right"] = window_info.right position["bottom"] = window_info.bottom # only print on updates if last != position: print(position) # copy(!!) the position last = {**position} if __name__ == "__main__": # setup pygame pg.init() # create a window win = pg.display.set_mode((600, 200)) # run main loop main(win)If you're NOT on Windows, there's almost definitely a way to do it. I just wouldn't know what it is haha.