你好我已經寫了一個.pyw腳本來自動重新啓動我的電腦,如果我閒置了幾秒鐘。出於某種原因,該腳本僅爲idleTime返回值0.328,而不是實際計算秒爲空閒的秒數。真的不知道哪裏這個問題在於所以這是我的代碼Python自動重啓電腦
import os
import datetime
from ctypes import Structure, windll, c_uint, sizeof, byref
#Script will automatically restart computer at 4 am unless user
#hits abort button.
os.system("start C:/Users/alexa/Desktop/test.txt")
#checks how long user has been idle for
class LASTINPUTINFO(Structure):
_fields_ = [
('cbSize', c_uint),
('dwTime', c_uint),
]
def get_idle_duration():
lastInputInfo = LASTINPUTINFO()
lastInputInfo.cbSize = sizeof(lastInputInfo)
windll.user32.GetLastInputInfo(byref(lastInputInfo))
millis = windll.kernel32.GetTickCount() - lastInputInfo.dwTime
return millis/1000.0
idleTime = get_idle_duration()
while(idleTime <= 30):
print(idleTime)
if(idleTime >= 30):
os.system("shutdown")