python获取屏幕信息

 1 """
 2 功能:识别两块显示器各自的分辨率
 3 """
 4 """模块导入"""
 5 from win32api import GetSystemMetrics
 6 from win32con import SM_CMONITORS, SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN
 7 
 8 def Display_Detection():
 9     # 显示器数量检测
10     MonitorNumber = GetSystemMetrics(SM_CMONITORS)
11     print('--------显示器数量:',MonitorNumber)
12     # 主屏幕尺寸检测
13     MajorScreenWidth = GetSystemMetrics(0)  # 主屏幕宽
14     MajorScreenHeight = GetSystemMetrics(1)  # 主屏幕高
15     print("主屏幕尺寸:", GetSystemMetrics(0), "*", GetSystemMetrics(1))
16     # 屏幕最大尺寸
17     aScreenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN)  # 屏幕最大宽度
18     aScreenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN)  # 屏幕最大高度
19     AllScreen=(aScreenWidth, aScreenHeight)
20     print("屏幕总尺寸:", aScreenWidth, "*", aScreenHeight)
21     # 当前主流的分辨率基数是宽,偶数是高
22     ResolvingPower = [1280, 720, 1920, 1080, 2560, 1440, 3840, 2160, 4096, 2160, 7680, 4320]
23 
24     if MonitorNumber > 1:  # 屏幕数量判断print(MonitorNumber)就可以知道有多少块屏幕
25         SecondaryScreenWidth = aScreenWidth - MajorScreenWidth  # 副屏宽=总屏幕宽-当前屏幕宽
26         print("副屏宽",SecondaryScreenWidth)
27 
28         # 主屏横竖屏检测
29         if GetSystemMetrics(0) > GetSystemMetrics(1):
30             MianScreen = (GetSystemMetrics(0), GetSystemMetrics(1))
31             print("主屏(横屏)尺寸:", GetSystemMetrics(0), "*", GetSystemMetrics(1))
32         else:
33             MianScreen = (GetSystemMetrics(0), GetSystemMetrics(1))
34             print("主屏(竖屏)尺寸:", GetSystemMetrics(0), "*", GetSystemMetrics(1))
35 
36         # 横屏状态
37         for i in range(0, len(ResolvingPower) - 1, 2):
38             # print("i",ResolvingPower[i])
39             if SecondaryScreenWidth == ResolvingPower[i]:
40                 SecondaryScreen = (ResolvingPower[i], ResolvingPower[i + 1])
41                 print("副屏(横屏)尺寸:", ResolvingPower[i], ResolvingPower[i + 1])
42                 # return "副屏(竖屏)尺寸:",SecondaryScreen
43                 break
44         # 竖屏状态
45         for i in range(1, len(ResolvingPower) - 1, 2):
46             # print("i",ResolvingPower[i])
47             if SecondaryScreenWidth == ResolvingPower[i]:
48                 SecondaryScreen = (ResolvingPower[i], ResolvingPower[i + 1])
49                 print("副屏(竖屏)尺寸:", ResolvingPower[i], ResolvingPower[i - 1])
50                 # return "副屏(竖屏)尺寸",SecondaryScreen
51                 break
52     # return MonitorNumber,AllScreen,MianScreen,SecondaryScreen
53     return MonitorNumber, MianScreen, SecondaryScreen
54 
55 #调用
56 a=Display_Detection()
57 print('---------------------------')
58 print(a)#a可以任意遍历其中的内容a[0]代表屏幕数量等等...

 

posted @ 2024-11-12 13:59  Pynix  阅读(22)  评论(0编辑  收藏  举报