Python Forum
pygame get window position
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pygame get window position
#4
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:
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.
metulburr likes this post
Reply


Messages In This Thread
pygame get window position - by vskarica - Oct-11-2020, 09:10 PM
RE: pygame get window position - by metulburr - Oct-11-2020, 11:48 PM
RE: pygame get window position - by vskarica - Oct-12-2020, 07:04 AM
RE: pygame get window position - by nilamo - Oct-18-2020, 05:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Cannot display anything inside pygame window the_gullwing 5 1,325 Dec-07-2023, 02:44 AM
Last Post: Benixon
  [PyGame] drawing images onto pygame window djwilson0495 1 3,558 Feb-22-2021, 05:39 PM
Last Post: nilamo
  Unable to exit Pygame window Hesper 2 3,951 Mar-30-2020, 08:59 AM
Last Post: Hesper
  [pyGame] More Text in one Window, example needed ! JamieVanCadsand 1 3,607 Oct-03-2017, 03:42 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020