12

How can I determine the monitor refresh rate?

 2 years ago
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.
neoserver,ios ssh client

How can I determine the monitor refresh rate?

Asked 12 years, 6 months ago
Active 3 days ago
Viewed 4k times

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.

asked Aug 3 2009 at 23:07

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
answered Aug 4 2009 at 4:40

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

answered Feb 4 at 23:50

You can use the pygame clock

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

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

answered Aug 18 2020 at 13:02

Your Answer

Sign up or log in

Sign up using Google
Sign up using Facebook
Sign up using Email and Password

Post as a guest

Name
Email

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK