Home  >  Article  >  Backend Development  >  How Do I Retrieve My Monitor Resolution Using Python?

How Do I Retrieve My Monitor Resolution Using Python?

Barbara Streisand
Barbara StreisandOriginal
2024-11-05 21:05:03532browse

How Do I Retrieve My Monitor Resolution Using Python?

Getting Monitor Resolution in Python

Determining the resolution of your monitor is often required for various applications. In Python, there are several approaches to retrieve this information, but perhaps the simplest is through the screeninfo module.

PyPI Module: Screeninfo

For added convenience, the screeninfo module is available on PyPI, allowing for easy installation:

pip install screeninfo

Usage:

Once installed, you can access your monitor resolution using the following code:

from screeninfo import get_monitors
for m in get_monitors():
    print(str(m))

Example Result:

Running this code will print information about each monitor connected to your system, including their x and y coordinates, width and height, and various other properties. For instance, you might see output similar to:

Monitor(x=3840, y=0, width=3840, height=2160, width_mm=1420, height_mm=800, name='HDMI-0', is_primary=False)
Monitor(x=0, y=0, width=3840, height=2160, width_mm=708, height_mm=399, name='DP-0', is_primary=True)

This information is presented as a tuple, providing both the width and height of each monitor. Additionally, the screeninfo module can handle multi-monitor setups, making it a versatile solution for determining monitor resolution in Python.

The above is the detailed content of How Do I Retrieve My Monitor Resolution Using Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn