如何在 python 中获得我的所有系统支持的屏幕分辨率

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

我想获得我的 windows 系统可以支持的所有可能屏幕分辨率的列表。我搜索了很多东西,但我能得到的最好的是使用 Pygame.

我想请问有没有其他方法或者如何使用这个pygame库来找到所有分辨率

对于大多数 Windows 相关的东西,您可以直接使用 pywin32 模块使用 Windows API。

因此,要获得所有可能的屏幕分辨率,您可以使用 EnumDisplaySettings 函数。

简单示例:

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)

结果:

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


但是如果你想使用pygame,你必须先通过调用pygame.init()初始化pygame模块,像这样:

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)]
>>>