Python如何来识别两个显示器屏幕各自的分辨率
在使用Python做桌面开发时如何识别两个显示器屏幕的分辨率
很多朋友应该在做桌面开发时会遇到多屏下如何解决布局问题,下面实现的就是获取准确的屏幕分辨率由于这是获取的两个屏幕的分辨率,所以三个屏幕的话可以在这基础上进行逻辑思考
当前显示器的主流分辨率:
| 分辨率 | 宽高 |
|---|---|
| HD720p | 1280*720 |
| FHD1080p | 1920*1080 |
| QHD 2K | 2560*1440 |
| UHD 4K | 3840*2160 |
| QUHD 8K | 7680*4320 |
下面是实现代码
# -*- coding: utf-8 -*-
"""
功能:识别两块显示器各自的分辨率
"""
"""模块导入"""
from win32api import GetSystemMetrics
from win32con import SM_CMONITORS, SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN#显示器数量检测
MonitorNumber = GetSystemMetrics(SM_CMONITORS)
#主屏幕尺寸检测
MajorScreenWidth=GetSystemMetrics(0)#主屏幕宽
MajorScreenHeight=GetSystemMetrics(1)#主屏幕高
print("主屏幕尺寸:", GetSystemMetrics(0),"*", GetSystemMetrics(1))
# 屏幕最大尺寸
aScreenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN) # 屏幕最大宽度
aScreenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN) # 屏幕最大高度
print("屏幕总尺寸:",aScreenWidth,"*",aScreenHeight)
#当前主流的分辨率基数是宽,偶数是高
ResolvingPower=[1280,720,1920,1080,2560,1440,3840,2160,4096,2160,7680,4320]if MonitorNumber > 1:#屏幕数量判断print(MonitorNumber)就可以知道有多少块屏幕SecondaryScreenWidth=aScreenWidth-MajorScreenWidth#副屏宽=总屏幕宽-当前屏幕宽# print("副屏宽",SecondaryScreenWidth)#主屏横竖屏检测if GetSystemMetrics(0)>GetSystemMetrics(1):print("主屏幕是横屏")else:print("主屏幕是竖屏")#横屏状态for i in range(0, len(ResolvingPower) - 1,2):# print("i",ResolvingPower[i])if SecondaryScreenWidth == ResolvingPower[i]:print("副屏(横屏)尺寸:", ResolvingPower[i], ResolvingPower[i + 1])break#竖屏状态for i in range(1, len(ResolvingPower) - 1,2):# print("i",ResolvingPower[i])if SecondaryScreenWidth == ResolvingPower[i]:print("副屏(竖屏)尺寸:", ResolvingPower[i], ResolvingPower[i - 1])break
为了方便还是将其模块化吧
# -*- coding: utf-8 -*-
"""
功能:识别两块显示器各自的分辨率
"""
"""模块导入"""
from win32api import GetSystemMetrics
from win32con import SM_CMONITORS, SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREENdef Display_Detection():# 显示器数量检测MonitorNumber = GetSystemMetrics(SM_CMONITORS)# 主屏幕尺寸检测MajorScreenWidth = GetSystemMetrics(0) # 主屏幕宽MajorScreenHeight = GetSystemMetrics(1) # 主屏幕高# print("主屏幕尺寸:", GetSystemMetrics(0), "*", GetSystemMetrics(1))# 屏幕最大尺寸aScreenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN) # 屏幕最大宽度aScreenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN) # 屏幕最大高度AllScreen=(aScreenWidth, aScreenHeight)# print("屏幕总尺寸:", aScreenWidth, "*", aScreenHeight)# 当前主流的分辨率基数是宽,偶数是高ResolvingPower = [1280, 720, 1920, 1080, 2560, 1440, 3840, 2160, 4096, 2160, 7680, 4320]if MonitorNumber > 1: # 屏幕数量判断print(MonitorNumber)就可以知道有多少块屏幕SecondaryScreenWidth = aScreenWidth - MajorScreenWidth # 副屏宽=总屏幕宽-当前屏幕宽# print("副屏宽",SecondaryScreenWidth)# 主屏横竖屏检测if GetSystemMetrics(0) > GetSystemMetrics(1):MianScreen = (GetSystemMetrics(0), GetSystemMetrics(1))# print("主屏(横屏)尺寸:", GetSystemMetrics(0), "*", GetSystemMetrics(1))else:MianScreen = (GetSystemMetrics(0), GetSystemMetrics(1))# print("主屏(竖屏)尺寸:", GetSystemMetrics(0), "*", GetSystemMetrics(1))# 横屏状态for i in range(0, len(ResolvingPower) - 1, 2):# print("i",ResolvingPower[i])if SecondaryScreenWidth == ResolvingPower[i]:SecondaryScreen = (ResolvingPower[i], ResolvingPower[i + 1])# print("副屏(横屏)尺寸:", ResolvingPower[i], ResolvingPower[i + 1])# return "副屏(竖屏)尺寸:",SecondaryScreenbreak# 竖屏状态for i in range(1, len(ResolvingPower) - 1, 2):# print("i",ResolvingPower[i])if SecondaryScreenWidth == ResolvingPower[i]:SecondaryScreen = (ResolvingPower[i], ResolvingPower[i + 1])# print("副屏(竖屏)尺寸:", ResolvingPower[i], ResolvingPower[i - 1])# return "副屏(竖屏)尺寸",SecondaryScreenbreakreturn MonitorNumber,AllScreen,MianScreen,SecondaryScreen#调用
a=Display_Detection()
print(a)#a可以任意遍历其中的内容a[0]代表屏幕数量等等...#(2, (4480, 1440), (2560, 1440), (1920, 1080))#运行结果:屏幕数量、总屏幕尺寸、主屏幕尺寸、副屏尺寸
这就是全部的内容了,如果有错误欢迎慷慨指正!
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
