pywin32
方法一
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}")
讯享网
方法二
讯享网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}")
方法三
import win32api screen_width = win32api.GetSystemMetrics(0) screen_height = win32api.GetSystemMetrics(1) print(f"屏幕分辨率为:{screen_width} x {screen_height}")
Pillow
需要注意的是,此方法需要安装 Pillow 模块。可以通过运行 pip install Pillow 命令来安装。使用 ImageGrab 模块的 grab() 方法来获取整个屏幕的截图,然后从截图中读取宽度和高度以获取屏幕分辨率。
讯享网from PIL import ImageGrab screen = ImageGrab.grab() screen_width, screen_height = screen.size print(f"屏幕分辨率为:{screen_width} x {screen_height}")
ctypes
需要注意的是,此方法仅适用于 Windows 操作系统,并且并未安装任何第三方库。通过调用 Windows 用户界面库中的 GetSystemMetrics 函数来获取屏幕分辨率。
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}")
运行此代码将输出屏幕分辨率。需要注意的是,pygetwindow 模块需要安装。可以通过运行 pip install pygetwindow 命令来安装。
screeninfo
from screeninfo import get_monitors for m in get_monitors(): print(f"屏幕分辨率为:{m.width} x {m.height}")
运行此代码将输出屏幕分辨率。需要注意的是,screeninfo 模块需要安装。可以通过运行 pip install screeninfo 命令来安装。
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}")
运行此代码将输出屏幕分辨率。需要注意的是,tkinter 需要在图形界面环境中才能正常工作,因此此方法可能不适用于某些应用程序或服务器环境。
pyautogui
import pyautogui screen_width, screen_height = pyautogui.size() print(f"屏幕分辨率为:{screen_width} x {screen_height}")
运行此代码将输出屏幕分辨率。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/47571.html