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.

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.

I am using Windows 10, in case that matters.

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

解决方案

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()

The SC_MONITORPOWER arguments are documented here.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值