

How can I determine the monitor refresh rate?
source link: https://stackoverflow.com/questions/1225057/how-can-i-determine-the-monitor-refresh-rate
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How can I determine the monitor refresh rate?
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.
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
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.)
On Linux, you can use 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)
print(randr.get_screen_info(info.root).rate)
Associating the appropriate screen index number with the place you want your PyOpenGL window to open up on is left as an exercise for the reader :).
(I won't duplicate Anurag's answer for Windows here, especially since I can't test it, but that's how you would do it on that platform.)
You can use the pygame clock
fps = 60
clock = pygame.time.Clock()
At the end of the code:
clock.tick(fps)
Your Answer
Post as a guest
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged python pygame pyopengl or ask your own question.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK