pywin32
method one
Use the win32api.GetDeviceCaps() method to get the display resolution.
Use the win32api.GetDC() method to get the device context handle of the entire screen, and then use the win32api.GetDeviceCaps() method to get the horizontal and vertical resolutions. Finally, you need to call the win32api.ReleaseDC() method to release the device context handle.
import win32api
import win32con
hdc = win32api.GetDC(0)
screen_width = win32api.GetDeviceCaps(hdc, win32con.HORZRES)
screen_height = win32api.GetDeviceCaps(hdc, win32con.VERTRES)
win32api.ReleaseDC(0, hdc)
print(f"屏幕分辨率为:{screen_width} x {screen_height}")
Method Two
Use the win32api.EnumDisplayMonitors() method to enumerate all monitors and get their resolutions.
This method returns a list where each element represents a rectangular area of the display. From the rectangular area, the width and height of the display can be calculated to determine its resolution.
import win32api
import win32con
def get_monitor_info():
monitors = []
monitor_enum_proc = lambda hMonitor, hdcMonitor, lprcMonitor, dwData: monitors.append(lprcMonitor)
win32api.EnumDisplayMonitors(None, None, monitor_enum_proc, 0)
return monitors
monitors = get_monitor_info()
for i, monitor in enumerate(monitors):
width = monitor[2] - monitor[0]
height = monitor[3] - monitor[1]
print(f"第{i+1}个显示器分辨率为:{width} x {height}")
method three
import win32api
screen_width = win32api.GetSystemMetrics(0)
screen_height = win32api.GetSystemMetrics(1)
print(f"屏幕分辨率为:{screen_width} x {screen_height}")
Pillow
Note that this method requires the Pillow module to be installed. It can be installed by running pip install Pillow
the command. Use the grab() method of the ImageGrab module to get a screenshot of the entire screen, then read the width and height from the screenshot to get the screen resolution.
from PIL import ImageGrab
screen = ImageGrab.grab()
screen_width, screen_height = screen.size
print(f"屏幕分辨率为:{screen_width} x {screen_height}")
ctypes
It should be noted that this method is only applicable to the Windows operating system, and does not install any third-party libraries. Get the screen resolution by calling the GetSystemMetrics function in the Windows User Interface Library.
import ctypes
user32 = ctypes.windll.user32
screen_width = user32.GetSystemMetrics(0)
screen_height = user32.GetSystemMetrics(1)
print(f"屏幕分辨率为:{screen_width} x {screen_height}")
pygetwindow
import pygetwindow as gw
screen = gw.getWindowsWithTitle('')[0]
screen_width, screen_height = screen.size
print(f"屏幕分辨率为:{screen_width} x {screen_height}")
Running this code will output the screen resolution. Note that pygetwindow
the module needs to be installed. It can be installed by running pip install pygetwindow
the command.
screeninfo
from screeninfo import get_monitors
for m in get_monitors():
print(f"屏幕分辨率为:{m.width} x {m.height}")
Running this code will output the screen resolution. Note that screeninfo
the module needs to be installed. It can be installed by running pip install screeninfo
the command.
tkinter
import tkinter as tk
root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
print(f"屏幕分辨率为:{screen_width} x {screen_height}")
Running this code will output the screen resolution. It should be noted that tkinter
it needs to work properly in a GUI environment, so this method may not be suitable for some applications or server environments.
pyautogui
import pyautogui
screen_width, screen_height = pyautogui.size()
print(f"屏幕分辨率为:{screen_width} x {screen_height}")
Running this code will output the screen resolution.