Many of the answers are already scattered around StackOverflow and can be summarized as follows.
To get the resolution on Windows in a purely pythonic fashion (reference: https://mcmap.net/q/127208/-how-do-i-get-monitor-resolution-in-python):
import ctypes
user32 = ctypes.windll.user32
screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
The MacOS solution also uses Python, but uses a package outside the standard library (reference: https://mcmap.net/q/127208/-how-do-i-get-monitor-resolution-in-python):
import AppKit
[(screen.frame().size.width, screen.frame().size.height)
for screen in AppKit.NSScreen.screens()]
Apparently the list comprehension will iterate over the screens in a multiple monitor setup.
I think Alex Martelli's response to a related issue (https://mcmap.net/q/745633/-getting-monitor-size-in-python) is also notable. He uses:
pygame.display.list_modes()
[(1920, 1080), (1768, 992), (1680, 1050), (1600, 1200), (1600, 1024), (1600, 900
), (1440, 900), (1400, 1050), (1360, 768), (1280, 1024), (1280, 960), (1280, 800
), (1280, 768), (1280, 720), (1152, 864), (1024, 768), (800, 600), (720, 576), (
720, 480), (640, 480)]
to get a list of largest to smallest resolutions available (although pygame
would become a dependency if you went this route). Conversely, I suspect it would work just fine in a cross-platform setting. Furthermore, he mentions pygame.display.set_mode
for setting the resolution (docs: http://www.pygame.org/docs/ref/display.html#pygame.display.set_mode). Here's a snippet of the docs for set_mode
:
"The resolution argument is a pair of numbers representing the width and height. The flags argument is a collection of additional options. The depth argument represents the number of bits to use for color."
Maybe that will get you started. At the very least you could perhaps check the source code for set_mode
to see if there's some possible inspiration there if you cannot use it directly.
Other potentially useful ideas:
- You can do a crude platform check with
sys.platform
(docs: http://docs.python.org/2/library/sys.html#sys.platform). This returns 'darwin'
on MacOS.
- The bit architecture should be accessible with the Python
platform
module. If I run platform.architecture()
on my machine it returns a tuple: ('64bit', '')
(docs: http://docs.python.org/2/library/platform.html#platform.architecture)