【问题标题】:How to get info of all monitors with Python on Windows?如何在 Windows 上使用 Python 获取所有监视器的信息?
【发布时间】:2015-08-04 06:23:15
【问题描述】:

我需要一种方法来获取 Windows 和 Python 中所有连接的监视器的制造商名称数据字符串

我的最终目标是一个返回这个的函数,例如:

["Lenovo LTN116AT06407", "BenQ G615HDPL"]

我找到了一个软件 (madVR),它可以得到我需要的东西,但我不知道它是怎么做的。

【问题讨论】:

标签: python windows


【解决方案1】:

这可以通过从您的 python 脚本执行 PowerShell 命令来完成。 要发现计算机上的多显示器配置信息,请使用 powershell Get-WmiObject win32_desktopmonitor 命令。命令输出如下所示:

__GENUS                     : 2
__CLASS                     : Win32_DesktopMonitor
__SUPERCLASS                : CIM_DesktopMonitor
__DYNASTY                   : CIM_ManagedSystemElement
__RELPATH                   : Win32_DesktopMonitor.DeviceID="DesktopMonitor1"
__PROPERTY_COUNT            : 28
__DERIVATION                : {CIM_DesktopMonitor, CIM_Display, CIM_UserDevice,
                               CIM_LogicalDevice...}
__SERVER                    : SQUALL
__NAMESPACE                 : root\cimv2
__PATH                      : \\SQUALL\root\cimv2:Win32_DesktopMonitor.DeviceID
                              ="DesktopMonitor1"
Availability                : 3
Bandwidth                   : 
Caption                     : LG IPS237(Analog)
ConfigManagerErrorCode      : 0
ConfigManagerUserConfig     : False
CreationClassName           : Win32_DesktopMonitor
Description                 : LG IPS237(Analog)
DeviceID                    : DesktopMonitor1
DisplayType                 : 
ErrorCleared                : 
ErrorDescription            : 
InstallDate                 : 
IsLocked                    : 
LastErrorCode               : 
MonitorManufacturer         : LG
MonitorType                 : LG IPS237(Analog)
Name                        : LG IPS237(Analog)
PixelsPerXLogicalInch       : 96
PixelsPerYLogicalInch       : 96
PNPDeviceID                 : DISPLAY\GSM587D\5&2494DFB6&0&UID1048848
PowerManagementCapabilities : 
PowerManagementSupported    : 
ScreenHeight                : 1080
ScreenWidth                 : 1920
Status                      : OK
StatusInfo                  : 
SystemCreationClassName     : Win32_ComputerSystem
SystemName                  : SQUALL

我们需要得到 Name 部分,所以使用正则表达式。 最终代码为:

import subprocess
import re

proc = subprocess.Popen(['powershell', 'Get-WmiObject win32_desktopmonitor;'], stdout=subprocess.PIPE)
res = proc.communicate()
monitors = re.findall('(?s)\r\nName\s+:\s(.*?)\r\n', res[0].decode("utf-8"))
print(monitors)

结果是:

['LG IPS237(Analog)']

【讨论】:

  • 不起作用。我使用 powershell 命令只得到 3 个东西(名称、DeviceID 和 MonitorManufacter),它们太通用了:LCD 1366x768 作为名称。
  • 您使用什么操作系统版本?是否安装了正确的显示驱动程序?
  • 这是Windows 8.1,没有安装显示驱动,只有通用的。
  • Windows 10 没有什么不同,我的显示器名称显示在设置中,但在 powershell 中使用 Get-WmiObject win32_desktopmonitor 给我的信息更少。甚至没有分辨率。
猜你喜欢
  • 1970-01-01
  • 2011-03-29
  • 1970-01-01
  • 1970-01-01
  • 2011-12-04
  • 2023-01-21
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
相关资源
最近更新 更多