gpt4 book ai didi

Python自动重启电脑

转载 作者:行者123 更新时间:2023-11-30 22:35:38 25 4
gpt4 key购买 nike

大家好,我已经编写了一个 .pyw 脚本,如果我空闲了 x 秒,它会自动重新启动我的计算机。由于某种原因,脚本只返回idleTime值0.328,而不是实际计算空闲的秒数。不太确定这个问题出在哪里,所以这是我的代码

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")

最佳答案

你永远不会更新idleTime,你设置它一次,然后执行一个while循环......

关于Python自动重启电脑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44490642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com