Pygame Window Info
While working on my ‘Tablet Pressure‘ code, I realized I needed a way to be able to query the position of the active Pygame screen relative to the desktop. There didn’t seem to be any built-in way in Pygame to do this.
After doing some searching, I ran across Python‘s ctypes bindings. These let you tap into Windows (the OS) ‘window info’. Based on fiddling with that code, I’ve come up with a Python class that lets you query a few things:
- The extents of the active Pygame window relative to the desktop: top, bottom, left, and right coordinates.
- The extents of the active Pygame screen inside that window, relative to the desktop: top, bottom, left, and right coordinates.
- The width of the window border edges, and the title bar.
Download the source here:
- pygameWindowInfo_0_1.py – Windows OS only. Sorry
- Tested with Pygame 1.9.1, and Python 2.6.2
It comes with its own unit test: Executing from a shell will give you a floating resizable Pygame window, and a printout like this in the shell, showing its functionality:
Here’s the unit test code, showcasing its functionality:
# Unit Testing: if __name__ == "__main__": """Unit Test, will print window, screen and border thicknesses to the shell.""" pygame.init() # Must set this env var for PygameWindowInfo to work! os.environ['SDL_VIDEO_WINDOW_POS'] = "128,128" screen = pygame.display.set_mode((256, 256), pygame.RESIZABLE) pygame.display.set_caption("PygameWindowInfo v%s"%__version__) # Create our object: winInfo = PygameWindowInfo() # Main loop: looping = True while looping: # ***Must update our winInfo object during every loop*** to accurately # track window and display screen position winInfo.update() screen.fill(pygame.Color("black")) events = pygame.event.get() for event in events: if event.type == pygame.QUIT: looping = False if event.type == pygame.VIDEORESIZE: WIDTH, HEIGHT = event.size screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE) winPos = winInfo.getWindowPosition() screenPos = winInfo.getScreenPosition() # Print interesting data! print "Window -", for key in winPos.keys(): print "%s: %s"%(key, winPos[key]), print " | Screen -", for key in screenPos.keys(): print "%s: %s"%(key, screenPos[key]), print " | Title thickness: %s"%winInfo.titleThickness, print " | Border thickness: %s"%winInfo.borderThickness, print "\n", pygame.display.flip() pygame.quit()