I'm new to python and programming in general. I made a small script that gives me a notification when the battery reaches 100% or goes below 25%. It runs fine for a single instance. I'm trying to get it to always run i.e. that it sits in the tray and popups up the notification as per the conditions. I tried putting the whole thing through a "while True:" loop but that does not seem to help. Could you guys help me out?
Code:-
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```
You need to routinely call the battery() in while, nothing else. So, for example, considering the rest of your code remains the same:
while True:
battery()
time.sleep(1)