3
votes

Is there a cross platform way to get the monitor's refresh rate in python (2.6)? I'm using Pygame and PyOpenGL, if that helps.

I don't need to change the refresh rate, I just need to know what it is.

3
What are you going to use the refresh rate for, if I might ask? If you are writing a game loop or something, you generally don't need to use the refresh rate.Mike Daniels
When my app is running in full screen, v-sync is enabled which caps the fps at the refresh rate. V-sync isn't enabled in windowed mode, and the fps is many times faster. I want it to run at the same speed in windowed and fullscreen, so I want to set the maximum fps as the refresh rate of the monitor.Brad Zeis
You should not tie your game logic and your render rate together as much as you have. Look here for some alternative game loop constructions: dewitters.koonsolo.com/gameloop.htmlMike Daniels

3 Answers

5
votes

I am not sure about the platform you use, but on window you can use ctypes or win32api to get details about devices e.g. using 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)

output on my system:

\\.\DISPLAY1 Mobile Intel(R) 945GM Express Chipset Family
Color: 0
BitsPerPel: 8
DisplayFrequency: 60
0
votes

On macOS, you can use pyobjc to query the refresh rate of all the attached displays. You'll need the Cocoa (i.e. "AppKit") framework for this:

$ python3 -m venv screen-res
$ . ./screen-res/bin/activate
$ pip install pyobjc-framework-Cocoa

then, in Python:

from AppKit import NSScreen

for each in NSScreen.screens():
    print(f"{each.localizedName()}: {each.maximumFramesPerSecond()}Hz")

On my system, this produces:

LG HDR WQHD+: 120Hz
Built-in Retina Display: 120Hz

(Which is correct, my displays are indeed both set to a 120Hz refresh rate.)

-3
votes

You can use the pygame clock

fps = 60
clock = pygame.time.Clock()

At the end of the code: clock.tick(fps)