有没有一种跨平台的方法可以在 python (2.6) 中获取显示器的刷新率?我正在使用 Pygame 和 PyOpenGL,如果有帮助的话。
我不需要改变刷新率,我只需要知道它是什么。
我不确定您使用的平台,但在窗口上您可以使用 ctypes 或 win32api 来获取有关设备的详细信息,例如使用 win32api
import win32api
def printInfo(device):
print((device.DeviceName, device.DeviceString))
settings = win32api.EnumDisplaySettings(device.DeviceName, -1)
for varName in ['Color', 'BitsPerPel', 'DisplayFrequency']:
print("%s: %s"%(varName, getattr(settings, varName)))
device = win32api.EnumDisplayDevices()
printInfo(device)
我的系统输出:
\\.\DISPLAY1 Mobile Intel(R) 945GM Express Chipset Family
Color: 0
BitsPerPel: 8
DisplayFrequency: 60
pyobjc
查询所有连接显示器的刷新率。为此,您需要Cocoa
(即“AppKit”)框架:
$ python3 -m venv screen-res
$ . ./screen-res/bin/activate
$ pip install pyobjc-framework-Cocoa
然后,在 Python 中:
from AppKit import NSScreen
for each in NSScreen.screens():
print(f"{each.localizedName()}: {each.maximumFramesPerSecond()}Hz")
在我的系统上,这会产生:
LG HDR WQHD+: 120Hz
Built-in Retina Display: 120Hz
(这是正确的,我的显示器确实都设置为 120Hz 刷新率。)
python-xlib
:
from Xlib import display
from Xlib.ext import randr
d = display.Display()
default_screen = d.get_default_screen()
info = d.screen(default_screen)
resources = randr.get_screen_resources(info.root)
active_modes = set()
for crtc in resources.crtcs:
crtc_info = randr.get_crtc_info(info.root, crtc, resources.config_timestamp)
if crtc_info.mode:
active_modes.add(crtc_info.mode)
for mode in resources.modes:
if mode.id in active_modes:
print(mode.dot_clock / (mode.h_total * mode.v_total))
将适当的屏幕索引号与您希望打开 PyOpenGL 窗口的位置相关联留给读者作为练习:)。
(我不会在这里重复 Anurag 对 Windows 的回答,尤其是因为我无法对其进行测试,但这就是您在该平台上的做法。)
您可以使用 DEVMODEW,但这只适用于 Windows 机器。
使用 pip 和“pip install wmi”安装'wmi'
import ctypes
import wmi
from time import sleep
class DEVMODEW(ctypes.Structure): # Defines the DEVMODEW Structure
_fields_ = [
("dmDeviceName", ctypes.c_wchar * 32),
("dmSpecVersion", ctypes.c_uint16),
("dmDriverVersion", ctypes.c_uint16),
("dmSize", ctypes.c_uint32),
("dmDriverExtra", ctypes.c_uint16),
("dmFields", ctypes.c_uint32),
("dmPosition", ctypes.c_int32 * 2),
("dmDisplayOrientation", ctypes.c_uint32),
("dmDisplayFixedOutput", ctypes.c_uint32),
("dmColor", ctypes.c_short),
("dmDuplex", ctypes.c_short),
("dmYResolution", ctypes.c_short),
("dmTTOption", ctypes.c_short),
("dmCollate", ctypes.c_short),
("dmFormName", ctypes.c_wchar * 32),
("dmLogPixels", ctypes.c_uint16),
("dmBitsPerPel", ctypes.c_uint32),
("dmPelsWidth", ctypes.c_uint32),
("dmPelsHeight", ctypes.c_uint32),
("dmDisplayFlags", ctypes.c_uint32),
("dmDisplayFrequency", ctypes.c_uint32),
("dmICMMethod", ctypes.c_uint32),
("dmICMIntent", ctypes.c_uint32),
("dmMediaType", ctypes.c_uint32),
("dmDitherType", ctypes.c_uint32),
("dmReserved1", ctypes.c_uint32),
("dmReserved2", ctypes.c_uint32),
("dmPanningWidth", ctypes.c_uint32),
("dmPanningHeight", ctypes.c_uint32)
]
def get_monitor_refresh_rate():
c = wmi.WMI()
for monitor in c.Win32_VideoController():
return monitor.MaxRefreshRate
return None
refresh_rate = get_monitor_refresh_rate()
if refresh_rate is not None:
print(f"Monitor refresh rate: {refresh_rate} Hz")
else:
print("Cannot Detremine the Monitor Refresh Rate.")
sleep(10)
你可以使用pygame时钟
fps = 60
clock = pygame.time.Clock()
代码末尾:
clock.tick(fps)