python 調用windows api查看系統的電量

phpopen 9年前發布 | 7K 次閱讀 Python

通過調用GetSystemPowerStatus Windows Api來判斷AC Power是開啟還是關閉狀態。是一個python調用windows api的例子。

import os
import win32con
import sys
import time
from ctypes import *

class PowerClass(Structure):
    _fields_ = [('ACLineStatus', c_byte),
            ('BatteryFlag', c_byte),
            ('BatteryLifePercent', c_byte),
            ('Reserved1',c_byte),
            ('BatteryLifeTime',c_ulong),
            ('BatteryFullLifeTime',c_ulong)]

powerclass = PowerClass()

while True:
    result = windll.kernel32.GetSystemPowerStatus( byref(powerclass) )

    try:
        state = int(powerclass.ACLineStatus)
    except:
        state = 0 

    if state == 1:
        print 'Power is on...'
    else:
        print 'Power is off...\a'  #\a = bell sounds beep on computer

    print 'Sleeping for 5 seconds...'
    time.sleep(5)

 本文由用戶 phpopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!