我对 python 和一般编程都很陌生。我制作了一个小脚本,当电池电量达到 100% 或低于 25% 时,它会向我发出通知。它对于单个实例运行良好。我试图让它始终运行,即它位于托盘中并根据条件弹出通知。我尝试将整个事情通过“while True:”循环,但这似乎没有帮助。你们能帮我一下吗?
代码:-
import psutil
from win10toast import ToastNotifier
toaster=ToastNotifier()
while True:
def battery():
val=psutil.sensors_battery()
percent=val.percent
power=val.power_plugged
if percent<25 and power==False:
return ('We\'re low on power({}%),plug in the charger.'.format(percent))
elif percent>=100 and power==True:
return ('Fully charged ({}%),you can disconnect the charger now.'.format(percent))
try:
toaster.show_toast('BatteryMeter',battery(),'c:/users/sanoop/desktop/Battery.ico',duration=5)
except:
pass```
最佳答案
您需要定期调用battery(),仅此而已。 因此,例如,考虑到代码的其余部分保持不变:
while True:
battery()
time.sleep(1)
关于python - 我的脚本运行一次,如何编码使其始终运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58883535/