Source:
(i) www.geeksforgeeks.org/create-battery-notifier-for-laptop-using-python
(ii) stackoverflow.com - question - doing-something-before-program-exit.
NOTE: First enable 'Notifications' in your Windows PC. (Click on the Clock icon, at the right corner in the status bar and turn off the 'Do not disturb' button. Web link: plyer-notifications-are-not-visible
#Save this Python program as 'BatteryNotifier.py' in your 'Users' folder, and run this program in DOS #prompt, and keep the DOS window open as long as you are working on your PC.
# Install and Import the 'psutil' module, for accessing info on running processes and system utilization.
# Install and Import the 'plyer' module, for accessing features of the Hardware, like displaying Notifications on the Desktop.
------------------------------------------------------------------------------------------------------------
import psutil
from plyer import notification
import time
import sys
import atexit
import signal
#To handle killing the process with kill or Ctrl+C.
def kill_handler(*args):
print("BatteryNotifier is turned OFF")
sys.exit(0)
signal.signal(signal.SIGINT, kill_handler)
signal.signal(signal.SIGTERM, kill_handler)
print("Battery Notifier is ON!!")
#from psutil we use the sensors_battery.
while(True):
battery = psutil.sensors_battery()
percent = battery.percent
if percent < 70:
notification.notify(title="Recharge battery!! ", message=str(percent)+"% Battery remaining", timeout=30)
time.sleep(35)
if battery.power_plugged:
print("Power is plugged IN")
else:
notification.notify(message = "Power is plugged OUT", timeout=30)
if percent > 97:
notification.notify(title="Battery Full!! ", message=str(percent)+"% Battery remaining", timeout=30)
#after every 5 mins this loop will iterate & show the respective notifications as long as anyone of the battery conditions is met.
time.sleep(60*5)
continue
------------------------------------------------------------------------------------------------------------------
Output:
Console output at beginning of the program:
No comments:
Post a Comment