views:

414

answers:

5

What is the simplest way to get monitor resolution (Preferably in a tuple)?

+5  A: 
from win32api import GetSystemMetrics
print "width =", GetSystemMetrics (0)
print "height =",GetSystemMetrics (1)

From: http://bytes.com/topic/python/answers/618587-screen-size-resolution-win32-python

Robus
Awesome exactly what I was looking for!
Anteater7171
+2  A: 

Using Linux, the simplest way is to execute bash command

xrandr | grep '*'

and parse its output using regexp.

Also you can do it through PyGame: http://www.daniweb.com/forums/thread54881.html

Riateche
+7  A: 

If you're using wxWindows, it's:

import wx

wx.App(False) # The wx.App object must be created first!    
print wx.GetDisplaySize()

(returns a tuple).

Ned Batchelder
+1 for a cross-platform answer.
Xavier Ho
+6  A: 

In Windows, you can also use ctypes:

import ctypes
user32 = ctypes.windll.user32
screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)

so that you don't need to install the pywin32 package; it doesn't need anything that doesn't come with Python itself.

jcao219
+1 for the "Python-only" example
Wayne Werner
It works even on Wine.
J.F. Sebastian
+5  A: 

And for completeness, Mac OS X

import AppKit
[(screen.frame().size.width, screen.frame().size.height)
    for screen in AppKit.NSScreen.screens()]

will give you a list of tuples containing all screen sizes (if multiple monitors present)

cobbal