【问题标题】:How can I detect brightness changes using Python and WMI on Windows 10?如何在 Windows 10 上使用 Python 和 WMI 检测亮度变化?
【发布时间】:2020-01-26 01:39:15
【问题描述】:

我正在尝试使用一组名为 Windows Management Instrumentation (WMI) 的工具来检测笔记本电脑上的亮度变化,特别是一个名为 WmiMonitorBrightnessEvent 的事件类和一个 watch_for 侦听器。后者is described here的使用,以及WMI事件的文档is found here

c = wmi.WMI ()
watcher = c.watch_for (
  notification_type="Creation",
  wmi_class="Win32_Process",
  delay_secs=2,
  Name='calc.exe'
)
calc_created = watcher ()

但是,只有 timgolden 网站上的第二个示例有效,用于检测计算器是否打开和关闭。

Here is one reference 可能有用。

请指导我调整代码以检测笔记本电脑上的亮度变化! 顺便说一句,我在 Python 3 和 Windows 10 上,这是我运行的代码:

import wmi
c = wmi.WMI()
power_watcher = c.Win32_WmiMonitorBrightnessEvent.watch_for()
print(power_watcher)

我得到的错误是:

Traceback (most recent call last):
  File "C:\Users\Arno\AppData\Local\Programs\Python\Python38\lib\site-packages\wmi.py", line 1145, in __getattr__
    return self._cached_classes (attribute)
  File "C:\Users\Arno\AppData\Local\Programs\Python\Python38\lib\site-packages\wmi.py", line 1156, in _cached_classes
    self._classes_map[class_name] = _wmi_class (self, self._namespace.Get (class_name))
  File "<COMObject winmgmts:>", line 2, in Get
  File "C:\Users\Arno\AppData\Local\Programs\Python\Python38\lib\site-packages\win32com\client\dynamic.py", line 287, in _ApplyTypes_
    result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'SWbemServicesEx', 'Not found ', None, 0, -2147217406), None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "watcher4.py", line 3, in <module>
    power_watcher = c.Win32_WmiMonitorBrightnessEvent.watch_for()
  File "C:\Users\Arno\AppData\Local\Programs\Python\Python38\lib\site-packages\wmi.py", line 1147, in __getattr__
    return getattr (self._namespace, attribute)
  File "C:\Users\Arno\AppData\Local\Programs\Python\Python38\lib\site-packages\win32com\client\dynamic.py", line 527, in __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: winmgmts:.Win32_WmiMonitorBrightnessEvent

【问题讨论】:

标签: python windows events event-handling wmi


【解决方案1】:

这样解决了

import wmi
c = wmi.WMI(namespace='wmi')
print_job_watcher = c.WmiMonitorBrightnessEvent.watch_for (
  notification_type="Modification",
  delay_secs=1
)

event_happened = print_job_watcher ()
print(event_happened)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 2017-01-08
    • 2020-04-25
    • 2018-12-22
    • 2016-07-16
    • 2020-11-04
    • 2018-05-02
    相关资源
    最近更新 更多