如何在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,(1768,992),(1680,1050),(1600,1200),(1440,900),(1366,768),(1360,(1280,1024),800),720),(1176,664),(1152,864),(1024,(800,600),(720,576),480),(640,480)]
>>>