我如何在 Python 或 PyQt4 中捕获系统注销/关闭/锁定事件

我如何在 Python 或 PyQt4 中捕获系统注销/关闭/锁定事件

问题描述:

我在 .NET 中有一个应用程序,现在我正在使用 Python 和 PyQt 对其进行重新编码.在我的应用程序中,我需要知道系统事件(锁定、注销、重启和关机).我可以使用下面的代码段从 .NET Frame work 3.5 管理它

I have an application in .NET now i am recoding it using Python and PyQt. In my application i need to know system events (Lock,Logoff,Restart and Shutdown). I could able to manage it from .NET Frame work 3.5 using below snippit

AddHandler SystemEvents.SessionEnding, AddressOf ClosingApplication
AddHandler SystemEvents.SessionSwitch, AddressOf SessionSwitchEvent

根据系统,我需要做一些事情来更新我的数据库.

Depending on the system i need to do some stuff to update my Database.

谁能帮帮我...

您将需要使用 pywin32 库.它是 win32api 的包装类.然后您可以使用 SetConsoleCtrlHandler 来捕捉信号.

You are going to need to use the pywin32 library. It is a wrapper class for the win32api. You can then use the SetConsoleCtrlHandler to catch the signal.

要干净地捕获信号,您需要生成一个控制台窗口.为了让您的应用程序干净利落地运行,您可以在隐藏状态下运行控制台窗口.有关如何生成控制台窗口的更多信息,请参阅:Python - Windows 关机事件

To cleanly capture the signal you are going to need to spawn a console window. To make your application run cleanly, you can run the console window in a hidden state. For more information about how to spawn the console window see: Python - Windows Shutdown Events