I wrote a little script to track the battery capacity of my device. Mostly for learning Python.
I decided to use a loop to recognize low capacity. So far it is okay, but when I use a tkinter
messagebox
, the window does not disappear after I confirm the warning.
import os
from time import sleep
import tkinter as tk
from tkinter import *
from tkinter import messagebox
stat = "/sys_vars/status"
plugged = "PLUGGED"
unplugged = "UNPLUGGED"
batfile = "/sys_vars/capa"
root = tk.Tk()
root.withdraw()
def read(sysfile):
with open(sysfile, "r") as f:
return f.read()
def loopcheck():
plug = str(read(stat).strip())
bat = int(read(batfile))
if plug == plugged:
sleep(2)
if plug == unplugged and 6 <= bat <= 15:
messagebox.showwarning("Alert!","Battery Low!\nCharging Required!")
sleep(2)
if plug == unplugged and bat <= 5:
messagebox.showwarning("ALERT!","Battery ULTRA Low!\nCharging Required!")
sleep(2)
if __name__ == "__main__":
messagebox.showinfo("Running!","Battry Status Tracker is now running on machine")
while True:
loopcheck()
I expect that I confirm the warning and then the message show after the amount of seconds.
The variables and the short sleep-timer in the example are there for tests.