How to Get the Monitor Resolution in Python

Lakshay Kapoor Feb 26, 2025 Python
  1. Method 1: Using Tkinter
  2. Method 2: Using PyAutoGUI
  3. Method 3: Using Screeninfo
  4. Conclusion
  5. FAQ
How to Get the Monitor Resolution in Python

When working on projects that involve graphical user interfaces or visual displays, knowing the monitor resolution is crucial. Whether you’re developing a game, designing a web application, or simply creating a data visualization tool, understanding the dimensions of the display can significantly impact user experience. In this tutorial, we will explore various methods to retrieve the monitor resolution using Python. We’ll cover popular libraries like tkinter, pyautogui, and screeninfo, providing clear code examples and explanations. By the end of this article, you’ll be equipped with the knowledge to seamlessly obtain monitor resolution in your Python applications.

Method 1: Using Tkinter

Tkinter is the standard GUI toolkit for Python and comes pre-installed with most Python distributions. It’s an effective way to get the monitor resolution since it provides a simple interface to interact with the display.

Here’s how you can use Tkinter to get the monitor resolution:

import tkinter as tk

root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.destroy()

print(f"Screen Resolution: {screen_width} x {screen_height}")

Output:

Screen Resolution: 1920 x 1080

This code snippet creates a hidden Tkinter window to access the screen’s width and height. The winfo_screenwidth() and winfo_screenheight() methods fetch the respective dimensions. After retrieving the values, we destroy the window to free up resources. This method is straightforward and efficient, making it an excellent choice for desktop applications.

Method 2: Using PyAutoGUI

PyAutoGUI is a cross-platform GUI automation library for Python that can also be used to get the screen resolution. It’s particularly useful if you are automating tasks or need to interact with the screen in a more dynamic way.

Here’s how to use PyAutoGUI to find the monitor resolution:

import pyautogui

screen_size = pyautogui.size()
screen_width, screen_height = screen_size

print(f"Screen Resolution: {screen_width} x {screen_height}")

Output:

Screen Resolution: 1920 x 1080

In this example, we import the pyautogui library and use the size() function, which returns the screen width and height as a tuple. We then unpack the tuple into two variables and print the result. This method is particularly handy for scripts that also involve mouse and keyboard automation, as it integrates well with other functionalities provided by PyAutoGUI.

Method 3: Using Screeninfo

Screeninfo is a lightweight library specifically designed to retrieve information about the screen, including resolution. It works on multiple platforms, making it a versatile choice for developers.

To get the monitor resolution using Screeninfo, follow this example:

from screeninfo import get_monitors

monitors = get_monitors()
for monitor in monitors:
    print(f"Screen Resolution: {monitor.width} x {monitor.height}")

Output:

Screen Resolution: 1920 x 1080

In this code, we import the get_monitors function from the screeninfo library. This function returns a list of monitor objects, each containing properties like width and height. We loop through the list and print the resolution of each monitor. This method is particularly useful if you have a multi-monitor setup, as it allows you to retrieve information for all connected displays.

Conclusion

Retrieving monitor resolution in Python is a straightforward process, thanks to the various libraries available. Whether you choose Tkinter for its simplicity, PyAutoGUI for its automation capabilities, or Screeninfo for its focus on screen details, each method has its own advantages. Understanding your display’s dimensions can enhance user experience and ensure your applications look their best. Experiment with these methods to find which one fits your project needs best.

FAQ

  1. How do I install PyAutoGUI?
    You can install PyAutoGUI using pip with the command pip install pyautogui.

  2. Can I use these methods on a multi-monitor setup?
    Yes, the methods using Screeninfo and Tkinter will work on multi-monitor setups, allowing you to retrieve the resolution of each connected display.

  3. Is Tkinter available on all operating systems?
    Yes, Tkinter is included with Python installations on Windows, macOS, and most Linux distributions.

  4. What if I want to get the resolution in a specific unit like inches?
    You would need to convert the pixel dimensions to inches based on the screen’s DPI (dots per inch), which can be obtained using additional libraries or settings.

  5. Are there any performance considerations when using these libraries?
    Generally, these libraries are lightweight and should not cause performance issues. However, it’s good practice to avoid unnecessary calls, especially in loops or intensive applications.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Lakshay Kapoor avatar Lakshay Kapoor avatar

Lakshay Kapoor is a final year B.Tech Computer Science student at Amity University Noida. He is familiar with programming languages and their real-world applications (Python/R/C++). Deeply interested in the area of Data Sciences and Machine Learning.

LinkedIn