tkinter.messagebox doesn't retrigger in my while loop

50 Views Asked by At

I'm a beginner programmer and I'm trying to write some code that would show a messagebox whenever my battery level gets to either 1% or 100%. With the code below, I'm able to get the messagebox whenever I run the program at the same time as when my battery is either at 1 or 100. However, whenever it isn't, the messagebox doesn't appear, even when it eventually reaches either of those two numbers. My program keeps running as I have a While loop so it should trigger when it reaches 1 or 100 but it doesn't and I'm not sure why. Some help would be greatly appreciated. Thanks!

import os
import subprocess
from tkinter import *
import tkinter.messagebox

# Terminal command to get the Battery percentage
batLevel = subprocess.run(['''pmset -g batt | grep -Eo "\d+%" | cut -d% -f1'''], shell=True, capture_output=True, encoding="utf", errors="ignore")
batLevel = batLevel.stdout.split("\n")[0]
batLevel = int(batLevel)

# Popup Window
def popup(msg=""):
    root=Tk()
    tkinter.messagebox.showerror(title=None, message=msg)
    root.mainloop()

while True:
    if batLevel == 100:
        popup("BATTERY FULL")
    elif batLevel == 1:
        popup("LOW BATTERY")
    else:
        continue
0

There are 0 best solutions below