Python
monitor resolution
screen size
display settings
coding tutorial

How do I get monitor resolution in Python?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Getting monitor resolution in Python is simple on a local desktop machine, but the right API depends on what you actually need. Some programs only need the primary screen size, while others need multi-monitor coordinates, scaling awareness, or a safe fallback when no display exists at all.

Fastest Built-In Option with tkinter

If you only need the primary display size and you are running a normal desktop session, tkinter is the lightest solution because it ships with standard Python on many platforms.

python
1import tkinter as tk
2
3root = tk.Tk()
4root.withdraw()
5
6width = root.winfo_screenwidth()
7height = root.winfo_screenheight()
8
9print(width, height)
10root.destroy()

This is a good choice for small desktop tools and scripts that already create a local window. It is less attractive in server code, containerized jobs, or environments where a GUI toolkit is unavailable.

Getting Multi-Monitor Details with screeninfo

If your application needs each monitor's width, height, and origin coordinates, use a library designed for that job.

bash
pip install screeninfo
python
1from screeninfo import get_monitors
2
3for monitor in get_monitors():
4    print(
5        "name=", monitor.name,
6        "x=", monitor.x,
7        "y=", monitor.y,
8        "width=", monitor.width,
9        "height=", monitor.height,
10    )

This is more useful than a single primary-screen size when you place windows, capture screenshots, or align overlays on a secondary monitor. Multi-monitor layouts can have negative x values when a screen sits to the left of the primary display, so keeping the full coordinate data matters.

Reusing pyautogui in Automation Scripts

If you are already using pyautogui for automation, it can provide the current screen size without adding another dependency just for that one call.

bash
pip install pyautogui
python
1import pyautogui
2
3size = pyautogui.size()
4print(size.width, size.height)

This is convenient in test automation or desktop macro tools, but you should remember that some platforms require screen-recording or accessibility permissions before automation libraries work correctly.

Windows-Specific Fallback with ctypes

On Windows, you can call the platform API directly through ctypes when you want no third-party package and no GUI toolkit.

python
1import ctypes
2
3user32 = ctypes.windll.user32
4width = user32.GetSystemMetrics(0)
5height = user32.GetSystemMetrics(1)
6
7print(width, height)

That is a practical fallback for scripts that only target Windows. It is not portable, so do not use it as your only implementation if the code needs to run elsewhere.

Handling Headless Environments

Resolution lookups can fail in CI, SSH sessions, containers, or Linux servers without a display server. A small guard helps avoid confusing crashes.

python
1import os
2import sys
3
4
5def has_display() -> bool:
6    if sys.platform.startswith("win"):
7        return True
8    return bool(os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY"))
9
10
11if has_display():
12    print("A display appears to be available.")
13else:
14    print("No display detected.")

That check is not perfect for every desktop stack, but it is a useful defensive layer. If your script may run both locally and in CI, treat the absence of a display as a normal case rather than an exceptional one.

Using Resolution Data for Layout

Most code does not need a resolution value by itself; it needs a geometry calculation based on that value. Keep that logic in one helper so it is easy to test.

python
1def centered_geometry(screen_w: int, screen_h: int, win_w: int, win_h: int) -> tuple[int, int]:
2    x = max((screen_w - win_w) // 2, 0)
3    y = max((screen_h - win_h) // 2, 0)
4    return x, y
5
6
7print(centered_geometry(1920, 1080, 800, 600))

Centralizing the arithmetic reduces repeated off-by-one or negative-coordinate bugs in startup code.

Choosing the Right Method

A simple rule works well:

  • Use tkinter for basic local desktop scripts.
  • Use screeninfo when multi-monitor awareness matters.
  • Reuse pyautogui if it is already part of your automation stack.
  • Use a platform API such as Windows ctypes only when you intentionally target that platform.

The method should match the deployment environment as much as the technical requirement.

Common Pitfalls

The most common mistake is assuming there is always a display. That is false in CI and many containerized environments. Another is assuming the primary monitor size describes the whole desktop, which breaks on multi-monitor systems. Developers also run into issues when they ignore scaling and permissions, especially in automation scenarios where the library can see the display only after the operating system grants access.

Summary

  • 'tkinter is the simplest built-in option for primary-screen resolution.'
  • 'screeninfo is better when you need multi-monitor metadata.'
  • 'pyautogui is convenient in automation code that already depends on it.'
  • Headless environments need explicit handling instead of GUI assumptions.
  • Keep geometry calculations separate from monitor-detection code.

Course illustration
Course illustration

All Rights Reserved.