在 Python 中检测计算机/程序关闭? [英] Detecting computer/program shutdown in Python?
问题描述
我有一个 Python 脚本,它定期循环运行,对我的照明系统进行调整.当我关闭计算机时,我希望我的脚本能够检测到这一点,然后将灯完全关掉.
I have a Python script that runs in a loop regularly making adjustments to my lighting system. When I shut down my computer, I'd like my script to detect that, and turn off the lights altogether.
如何检测我的计算机开始在 Python 中关闭?
How do I detect my computer beginning to shut down in Python?
或者,假设 Windows 向 Python 发送关闭时间"通知,我如何拦截它以关闭我的灯并退出循环?
Or, assuming Windows sends Python a "time to shut down" notice, how do I intercept that to kill my lights and exit the loop?
推荐答案
你应该对 WM_ENDSESSION
消息.
You should react to the WM_ENDSESSION
message.
当用户注销或计算机关闭时发送此消息.
This message is sent when the user logs off or the computer gets shut down.
如果您还想对睡眠/休眠做出反应,则需要处理 WM_POWERBROADCAST
和 PBT_APMSUSPEND
.
If you want to react to Sleep/Hibernate as well, you'll need to handle WM_POWERBROADCAST
with PBT_APMSUSPEND
.
但我不知道如何在 python 中做到这一点.我想这取决于您的窗口框架,因为您需要有一个窗口/消息循环来接收消息.
But I don't know how to do that in python. I guess it depends on your windowing framework since you need have a windows/a message loop to receive messages.
这篇关于在 Python 中检测计算机/程序关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!