Getting Your Screen Resolution with Python

I was recently looking into ways to get my screen resolution with Python to help diagnose an issue with an application that wasn’t behaving correctly. In this article, we’ll look at some of the ways to get your screen resolution. Not all of the solutions will be cross-platform, but I’ll be sure to mention that fact when I discuss those methods. Let’s get started!

Using the Linux Command Line

There are several ways to get your screen resolution in Linux. If you do a Google search, you’ll see people using various Python GUI toolkits. I wanted to find a way to get the screen resolution without installing a third party module. I eventually found the following command:

xrandr | grep '*'

I then had to take that information and translate it into Python. Here’s what I came up with:

import subprocess

cmd = ['xrandr']
cmd2 = ['grep', '*']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
p2 = subprocess.Popen(cmd2, stdin=p.stdout, stdout=subprocess.PIPE)
p.stdout.close()

resolution_string, junk = p2.communicate()
resolution = resolution_string.split()[0]
width, height = resolution.split('x')

Whenever you need to pipe data with Python, you will need to create two different subprocess instances. That is what I did above. I piped the output from xrandr to my second subprocess via its stdin. Then I closed the stdout of the first process to basically flush whatever it had returned to the second process. The rest of the code just parses out the width and height of the monitor.

Using PyGTK

Of course the above method is Linux-only. If you happen to have PyGTK installed, then you can get your screen resolution using that. Let’s take a look:

import gtk

width = gtk.gdk.screen_width()
height = gtk.gdk.screen_height()

That was pretty straightforward as PyGTK has those methods built-in. Note that PyGTK is available for Windows and Linux. There is supposed to be a Mac version in development as well.

Using wxPython

As you might expect, the wxPython toolkit also provides a method for getting your screen resolution. It’s a bit less useful in that you actually need to create an App object before you can get the resolution though.

import wx

app = wx.App(False)
width, height = wx.GetDisplaySize()

This is still an easy way to get the resolution you’re looking for. It should also be noted that wxPython runs on all three major platforms.

Using Tkinter

The Tkinter library is usually included with Python, so you should have this one be default. It also provides screen resolution, although it requires you to create an “app” object too:

import Tkinter

root = Tkinter.Tk()
width = root.winfo_screenwidth()
height = root.winfo_screenheight()

Fortunately, Tkinter is also available on all 3 major platforms, so you can use this method pretty much anywhere.

Using PySide / PyQt

As you’ve probably guessed, you can also get the screen resolution using PySide and PyQt. Here’s the PySide version:

from PySide import QtGui

app = QtGui.QApplication([])
screen_resolution = app.desktop().screenGeometry()
width, height = screen_resolution.width(), screen_resolution.height()

If you have PyQt4 instead, then you’ll want to change the import at the beginning to the following:

from PyQt4 import QtGui

The rest is the same. And as you probably know, both of these libraries are available on Windows, Linux and Mac.

Wrapping Up

At this point, you should be able to get the screen resolution on any OS you’re on. There are additional methods of getting this information that are platform dependent. For example, on Windows you can use PyWin32’s win32api or ctypes. On Mac, there is AppKit. But the toolkit methods listed here will work on most platforms just fine and because they’re cross-platform, you won’t need to use special cases to import specific packages to make this work.

Additional Reading

10 thoughts on “Getting Your Screen Resolution with Python”

  1. Nice collection.

    My propositions. They should be platform independent.

    import pygame

    pygame.init()
    info = pygame.display.Info()
    width = info.current_w
    height = info.current_h

    # —–

    import pyautogui

    width, height = pyautogui.size()

  2. Most of my examples were platform independent. I just brought up the first one because I had a specific need to get the screen resolution on Linux only without being able to install 3rd party packages.

  3. For a one off script, I think it’s fine. I’ve looked at some implementations of grep in Python and they can get really messy. I have seen some neat uses of Python’s re module though, so maybe that’s the way to go.

  4. How messy is this?

    xrandr, _ = p.communicate()
    resolution_line = [l for l in xrandr.splitlines() if ‘*’ in l][0]

    resolution = resolution_string.split()[0]
    width, height = resolution.split(‘x’)

  5. HI, thank you for this. I use the PySide, and it works just fine. On my virtual mouse using hand gesture. My question is, how can i measure the whole resolution of the 4 monitors so that my mouse will move on other monitor. Im using extension monitors. Thanks

  6. For PySide, i use this for my virtual mouse using hand gesture. I have extended monitor, the problem is my mouse only works on the primary screen. How can i make this work on other monitor? Like extension. Thanks.

  7. Pingback: python - Come faccio a ottenere la risoluzione del monitor in Python?

  8. Pingback: python - ¿Cómo puedo obtener la resolución del monitor en Python?

Comments are closed.