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

Copyright © 2024 Mouse Vs Python | Powered by Pythonlibrary