Skip to content

Instantly share code, notes, and snippets.

@alcides
Created May 1, 2009 16:27
Show Gist options
  • Save alcides/105124 to your computer and use it in GitHub Desktop.
Save alcides/105124 to your computer and use it in GitHub Desktop.
Get screen resolution
import sys,os
# Defaults
SCREEN_WIDTH=300
SCREEN_HEIGHT=300
if sys.platform == "win32":
# Windows sucks, but has an API
from win32api import GetSystemMetrics
SCREEN_WIDTH = GetSystemMetrics(0)
SCREEN_HEIGHT = GetSystemMetrics(1)
elif sys.platform == "darwin":
# Mac OS X
# (Doesn't use the xdpyinfo since it doesn't work with more than one monitor)
from AppKit import *
s = NSScreen.mainScreen().frame().size
SCREEN_WIDTH = s.width
SCREEN_HEIGHT = s.height
else:
# X based OSes.
try:
out = os.popen2("xdpyinfo | grep 'dimensions:'")[1].read()
out = out.replace(" dimensions: ","")
i = out.find(" pixels")
out = out[:i]
out = out.split("x")
SCREEN_WIDTH=int(out[0])
SCREEN_HEIGHT=int(out[1])
except:
pass
if __name__ == '__main__':
print SCREEN_WIDTH, SCREEN_HEIGHT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment