Python Forum
pygame get window position
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pygame get window position
#1
Hi!
This seems like an obviously asked and answered and if so, I apologize. However, I cannot seem to be able to find the answer, and I have lost a few days searching.
So, I am trying to get current position of pygame window on physical screen in order to set other forms positions accordingly.
It would also bi nice to be able to set position and detect movement of window, but it’s not critical for my project. Not at this point anyway.
All I found was:
import os
os.environ['SDL_VIDEO_WINDOW_POS']='%d,%d' %(x,y)
Please help!
Reply
#2
Most of the time pygame is used as the sole GUI. It is not ideal to use with tkinter as people often do, for example. Pygame is designed to be the sole GUI while playing a game, not mixed with other GUI to be positioned on the monitor.
os.environ['SDL_VIDEO_WINDOW_POS']='{},{}'.format(100,200)
this sets the environmental variable for SDL which pygame uses to position the window in that spot. However this variable is static....it does not change as you move the window. It is only useful for setting the initial location of the window to be spawned.

All of pygame.display methods are pertaining to the window itself, is current size and width, etc. not its location on the screen.

To be honest you may have to ask the developers on this one.
Recommended Tutorials:
Reply
#3
Thank you for your answer.
I figured as much. It is too obvious a question to not have an answer somewhere however incompetent searcher one might be.
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Cannot display anything inside pygame window the_gullwing 5 1,147 Dec-07-2023, 02:44 AM
Last Post: Benixon
  [PyGame] drawing images onto pygame window djwilson0495 1 3,492 Feb-22-2021, 05:39 PM
Last Post: nilamo
  Unable to exit Pygame window Hesper 2 3,888 Mar-30-2020, 08:59 AM
Last Post: Hesper
  [pyGame] More Text in one Window, example needed ! JamieVanCadsand 1 3,545 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