如何检查是否使用Python打开/关闭设备的显示屏? [英] How to check whether the device's display is turned on/off with Python?

查看:454
本文介绍了如何检查是否使用Python打开/关闭设备的显示屏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个Python脚本来检查我的设备是否有显示器以及该显示器是打开还是关闭.

I want to write a Python script that checks whether my device has a display and whether that display is turned on or off.

我用Google搜索它,有一个名为"WMI"的第三方库,但是它只能获取一些信息,例如CPU/HDD/进程/线程,所以对此我感到困惑.

I googled it, there is a third-party library named "WMI", but it can only get some information like CPU/HDD/process/thread, so I am confused about it.

以防万一,我正在使用Windows 10.

I am using Windows 10, in case that matters.

是否可以通过Python获得这种低级别的硬件信息,如果可以的话,我该怎么办?

Is it possible to get that kind of low level hardware information via Python, and if it is, how can I do it?

推荐答案

Windows似乎真的没有办法告诉您显示器是打开还是关闭. WMI Win32_DesktopMonitor 类具有可用性"属性,但这似乎不受更改监视器状态的影响.我使用以下python脚本对此进行了测试:

It looks like Windows does not really have a way to tell you if the monitor is on or off. The WMI Win32_DesktopMonitor class has an 'Availability' property but this doesn't seem to be affected by changing the monitor state. I tested this using the following python script:

import wmi # pip install WMI
import win32gui, win32con
SC_MONITORPOWER = 0xF170

wmic = wmi.WMI()

def powersave():
    # Put the monitor to Off.
    win32gui.SendMessage(win32con.HWND_BROADCAST, win32con.WM_SYSCOMMAND, SC_MONITORPOWER, 2)
    # Get the monitor states
    print([monitor.Availability for monitor in wmic.Win32_DesktopMonitor()])

if __name__ == '__main__':
    powersave()

SC_MONITORPOWER参数已记录这里.

The SC_MONITORPOWER arguments are documented here.

不幸的是,我的显示器的结果始终为3,这意味着它是打开的",即使它实际上是在睡眠模式下还是在关机状态下都已关闭.

Unfortunately the result for my monitor is always 3 which means it is "on", even when it is actually powered down either in sleep mode or physically off.

根据您的要求,您可能只想发送广播消息来断言所需的电源状态,而无需检查当前状态.

Depending on your requirement, you might just want to send the broadcast message to assert the power state you want and not need to check the current state.

这篇关于如何检查是否使用Python打开/关闭设备的显示屏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆