![]() # Set shorter prompt to better fit when pasted in StackOverflow (or other) pages # More details about the bug (and fix, workaround): : Why is signed, but native windows BYTE is unsigned? answer).Īnyway, I submitted : mhammond/pywin32 - Add GetSystemPowerStatus wrapper for GetSystemPowerStatus function to be available in Win32API.īuilding win32api.pyd locally and overwriting the one from site-packages directory (as I mentioned in the Test section), yields: sopr.bat : In Python, how can I detect whether the computer is on battery power? answer) is portable and doesn't require extra packages, but it's negatively impacted (until Python v3.12) by a CTypes ( WinTypes) bug. On my system that prints this (basically meaning "desktop, plugged in"): ACLineStatus 1 ![]() Print('BatteryFullLifeTime', status.BatteryFullLifeTime) Print('BatteryLifeTime', status.BatteryLifeTime) Print('BatteryLifePercent', status.BatteryLifePercent) Print('ACLineStatus', status.ACLineStatus) If not GetSystemPowerStatus(ctypes.pointer(status)): GetSystemPowerStatus.restype = wintypes.BOOL SYSTEM_POWER_STATUS_P = ctypes.POINTER(SYSTEM_POWER_STATUS) # Get power status of the system using ctypes to call GetSystemPowerStatusĬlass SYSTEM_POWER_STATUS(ctypes.Structure): It's a tiny bit more work for GetSystemPowerStatus() because you have to define the SYSTEM_POWER_STATUS structure, but not bad. I usually run CPython without win32api, so I kinda like these solutions. If you want to do it without win32api, you can use the built-in ctypes module.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |