pygame get window position - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: pygame get window position (/thread-30200.html) |
pygame get window position - vskarica - Oct-11-2020 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! RE: pygame get window position - metulburr - Oct-11-2020 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. RE: pygame get window position - vskarica - Oct-12-2020 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. RE: pygame get window position - nilamo - Oct-18-2020 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. |