Search code examples
pythonwindowspygamescreenscreen-resolution

How to get all my system supported screen resolutions in python


>>> import pygame
pygame 2.0.0 (SDL 2.0.12, python 3.8.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> pygame.display.list_modes()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pygame.error: video system not initialized

I want to get a list of all possible screen resolutions my windows system can support. I searched a lot of things but best I can get is by using Pygame.

I want to ask if there are any other methods or how to use this pygame library to find all the resolutions


Solution

  • For most things that are Windows related, you can use the Windows API directly using the pywin32 module.

    So, for getting all possible screen resolutions, you can use the EnumDisplaySettings function.

    Simple example:

    import win32api
    
    i=0
    res=set()
    try:
      while True:
        ds=win32api.EnumDisplaySettings(None, i)
        res.add(f"{ds.PelsWidth}x{ds.PelsHeight}")
        i+=1
    except: pass
    
    print(res)
    

    Result:

    {'1920x1080', '1152x864', '1176x664', '1768x992', '800x600', '720x576', '1600x1200', '1680x1050', '1280x720', '1280x1024', '1280x800', '1440x900', '1366x768', '1280x768', '640x480', '720x480', '1024x768', '1360x768'}


    But if you want to use pygame, you have to initialize the pygame module first by calling pygame.init(), like this:

    Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pygame
    pygame 2.0.0.dev10 (SDL 2.0.12, python 3.7.3)
    Hello from the pygame community. https://www.pygame.org/contribute.html
    >>> pygame.init()
    (6, 0)
    >>> pygame.display.list_modes()
    [(1920, 1080), (1920, 1080), (1920, 1080), (1920, 1080), (1920, 1080), (1920, 1080), (1768, 992), (1768, 992), (1768, 992), (1680, 1050), (1680, 1050), (1600, 1200), (1440, 900), (1440, 900), (1366, 768), (1366, 768), (1360, 768), (1360, 768), (1280, 1024), (1280, 1024), (1280, 800), (1280, 800), (1280, 768), (1280, 768), (1280, 720), (1280, 720), (1280, 720), (1176, 664), (1176, 664), (1176, 664), (1152, 864), (1024, 768), (1024, 768), (1024, 768), (800, 600), (800, 600), (800, 600), (800, 600), (720, 576), (720, 480), (720, 480), (640, 480), (640, 480), (640, 480), (640, 480)]
    >>>