-
Notifications
You must be signed in to change notification settings - Fork 0
/
Battery Percentage.py
65 lines (45 loc) · 1.51 KB
/
Battery Percentage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import threading
import winsound
import psutil
import ctypes
import time
import sys
if psutil.sensors_battery() is None:
ctypes.windll.user32.MessageBoxW(0, "This Computer may not have a battery or may be damaged.", "Battery Status", 0)
sys.exit()
def batery_check():
battery = psutil.sensors_battery()
plugged = battery.power_plugged
percent = battery.percent
if not plugged:
plugged = "Not Plugged In"
else:
plugged = "Plugged In"
return percent, plugged
def beep():
frequency = 2500 # Set Frequency To 2500 Hertz
duration = 2000 # Set Duration To 1000 ms == 1 second
winsound.Beep(frequency, duration)
def messagebox():
ctypes.windll.user32.MessageBoxW(0, "Unplug Charger now to avoid battery damage!", "Battery Status", 0)
def check():
flag = True
messageflag = False
while flag:
percent, plugged = batery_check()
sys.stdout.flush()
sys.stdout.write('\rRunning program, battery at ' + str(percent) + "% " + plugged)
sys.stdout.flush()
if plugged == "Plugged In":
if percent >= 95:
beep()
if not messageflag:
message = threading.Thread(target=messagebox)
message.start()
messageflag = True
else:
if percent <= 95:
messageflag = False
time.sleep(2)
t = threading.Thread(target=check)
t.start()