Retrieving Screen Resolution in Python: 4 Effective Methods

HOW TO GET SCREEN RESOLUTION IN PYTHON

Screen resolution is the number of pixels that can be displayed in each dimension on your computer screen or monitor. It is important to know the screen resolution of your computer because it helps us analyse the quality of videos and images. If the resolution of the screen is high then it can process greater pixel information and can provide high end graphics.

Knowing your screen resolution helps you to understand what kind of images your computer can display with how much clarity. In programming, knowing your screen resolution is essential to comprehend how much output can fit on your screen.

People who design games need clear screen resolution details to analyse the amount of graphic they can fit on the display screen and write code accordingly.

There are many ways in which you can find out the screen resolution via Python. You can do it using some special modules or without them. We’ll look at some of those methods in this tutorial in the following sections.

Method 1: Pyautogui module

The Pyautogui module is a Python module that can be used across all operating systems such as windows, macOS or Linux to recognize mouse or cursor movements or keyboard clicks. It is used for a number of purposes mainly in order to design UI systems.

When you install Python, the Pyautogui module is supposed to be pre installed with some other packages. But if for some reason your system doesn’t have this library, run the following code in your command prompt in administrator mode.

pip install pyautogui

Your installation should look something like this:

Glimpse Of Pyautogui Installation Process
Glimpse Of Pyautogui Installation Process

Now you just need to import this module into your Python shell and you can easily get the monitor resolution in the form of (width, height) as a tuple.

>>import pyautogui
>>pyautogui.size()
Size(width=1366, height=768)

Related: Automation with PyAutoGUI in Python.

Method 2: win32api module

The win32api in Python is used for creating and deploying 32 bit applications using python. It can also be used for getting the screen resolution as well. They are mainly used for configuring administrative system files in your computer.

This module can be installed by running the following code in your command prompt or shell in administrator mode using pip if you’re using windows. This module works for all operating systems.

pip install pywin32

Now, open your Python shell and import the module and run the following code:

>>import win32api
>>from win32api import GetSystemMetrics
>>print('The Width is= ', GetSystemMetrics(0))
The Width is= 1366 #output for width
>>print('The Height is= ', GetSystemMetrics(1))
The Height is= 768 #output for height

So, in the shell, we have got the width and height of our screen as required as shown above.

Using Win32APi And GetSystemMetrics
Using Win32APi And GetSystemMetrics

Method 3: wxPython module

The wx module in Python is used as an alternate to tkinter as an extension module. It is a wrapper for the GUI based application wxwidgets for Python. You can install it by running the following command in your windows system, although it can be used for all other operating systems as well.

pip install wxPython

Now you can use this module to get the screen resolution in Python.

import wx
outp= wx.App(False)
width, height= wx.GetDisplaySize()
print("The width is= " + str(width) + " and the height is= " + str(height))

The output would be as follows:

The width is= 1366 and the height is= 768
Using The Wx Wrapper To Find Our Screen Resolution
Using The Wx Wrapper To Find Our Screen Resolution

Method 4: Tkinter

The tkinter module is one of the most popular GUI tool that is used to create widgets using C commands in python. It is very popular among developers for creating User interface designs. You can also use this module to check your screen resolution by creating an object and then following the rest of the code:

import tkinter
app = tkinter.Tk()
width = app.winfo_screenwidth()
height = app.winfo_screenheight()
print("width=",width)
print("height=",height)

The output would be something like this:

width= 1366
height= 768

Do check out: Tkinter Tutorial – Drawing a Line Using Tkinter Canvas.

Conclusion.

In this article, we have gone through 4 of the easiest methods to determine our screen resolution. Screen resolution information is important for a variety of reasons such as, for game development, for creating user interfaces, etc. Due to the vast availability of Python modules, determining your monitor’s information is easier than you think. Which of these methods do you think you’re going to use in your next project?