当前位置: 首页 > news >正文

python --获取本机屏幕分辨率

pywin32

方法一

使用 win32api.GetDeviceCaps() 方法来获取显示器的分辨率。
使用 win32api.GetDC() 方法获取整个屏幕的设备上下文句柄,然后使用 win32api.GetDeviceCaps() 方法获取水平和垂直方向的分辨率。最后需要调用 win32api.ReleaseDC() 方法释放设备上下文句柄。

	import win32apiimport win32conhdc = 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}")

方法二

使用 win32api.EnumDisplayMonitors() 方法来枚举所有显示器并获取其分辨率。
此方法返回一个列表,其中每个元素代表一个显示器的矩形区域。可以从矩形区域计算出显示器的宽度和高度,从而确定其分辨率。

import win32api
import win32condef get_monitor_info():
monitors = []
monitor_enum_proc = lambda hMonitor, hdcMonitor, lprcMonitor, dwData: monitors.append(lprcMonitor)
win32api.EnumDisplayMonitors(None, None, monitor_enum_proc, 0)
return monitorsmonitors = 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 win32apiscreen_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 ImageGrabscreen = ImageGrab.grab()
screen_width, screen_height = screen.size
print(f"屏幕分辨率为:{screen_width} x {screen_height}")

ctypes

需要注意的是,此方法仅适用于 Windows 操作系统,并且并未安装任何第三方库。通过调用 Windows 用户界面库中的 GetSystemMetrics 函数来获取屏幕分辨率。

import ctypesuser32 = ctypes.windll.user32
screen_width = user32.GetSystemMetrics(0)
screen_height = user32.GetSystemMetrics(1)
print(f"屏幕分辨率为:{screen_width} x {screen_height}")

pygetwindow

import pygetwindow as gwscreen = 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_monitorsfor m in get_monitors():
print(f"屏幕分辨率为:{m.width} x {m.height}")

运行此代码将输出屏幕分辨率。需要注意的是,screeninfo 模块需要安装。可以通过运行 pip install screeninfo 命令来安装。

tkinter

import tkinter as tkroot = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
print(f"屏幕分辨率为:{screen_width} x {screen_height}")

运行此代码将输出屏幕分辨率。需要注意的是,tkinter 需要在图形界面环境中才能正常工作,因此此方法可能不适用于某些应用程序或服务器环境。

pyautogui

import pyautoguiscreen_width, screen_height = pyautogui.size()
print(f"屏幕分辨率为:{screen_width} x {screen_height}")

运行此代码将输出屏幕分辨率。

http://www.lryc.cn/news/45351.html

相关文章:

  • Java多态
  • 绝对路径和相对路径
  • Linux第二次总结
  • 算法:贪婪算法、分而治之
  • 462. 最小操作次数使数组元素相等 II——【Leetcode每日一题】
  • 对数据库的库及表的操作
  • final类又没实现接口应该用哪一种代理, jdk动态代理还是cglib代理
  • 使用StaMPS_Visualizer
  • 高并发-高性能-高可用-结论版
  • 数智转型助力建筑业全产业链升级,你了解多少?
  • Python网络设备脚本中经常使用的connecthandler和telnetlib是什么意思?
  • 你真的会写 git commit message 吗?
  • ISO文件内添加kickstart完成自动安装
  • SpringBoot 整合RabbitMq 自定义消息监听容器来实现消息批量处理
  • jquery基础之操作节点对象
  • 对于Java的深入理解及其特点--面试
  • Linux GPSD的使用
  • ArrayList无参构造添加元素源码解读
  • 手写简易 Spring(二)
  • 排列问题DFS入门
  • 【每日一题Day159】LC1638统计只差一个字符的子串数目 | 枚举
  • 【07 Metadata and VendorTag】
  • Golang中Model的使用
  • 交友项目【基础环境搭建】
  • 入职时,公司要求自己带电脑,每月给100元补贴,如果不接受就不能入职!
  • 20道经典Redis面试题
  • 十分钟带你看懂接口测试,2023最全超大型接口测试攻略
  • 【设计模式】创建型-单例模式
  • Python 练习 六
  • 「SQL面试题库」 No_22 员工奖金