0

I'm doing a battery reminder so I have to run my program in background.

def refresher():
    global proceed
    while proceed:
        battery = psutil.sensors_battery()
        print("Battery percentage : ", battery.percent)
        time.sleep(2)
        if battery.percent > 99:
            messagebox.showinfo("Reminder","Battery is full")
        elif battery.percent < 2:
            messagebox.showwarning("Reminder","Battery is running low.\nPlease charge it immediately.")

I want to run my tkinter app in the background even when the root windows is close like this.

enter image description here

I will export my tkinter app as exe by using auto-py-to-exe later.
Any suggestion or advice would help. Thanks.

  • 1
    Does this link help answer your question? Explains how to add a tkinter application to the system tray https://stackoverflow.com/questions/62372144/how-to-add-system-tray-to-my-tkinter-application-and-avoid-using-lots-of-pywin32 – scotty3785 Feb 07 '22 at 10:49
  • Note: I highly recommend using .after to schedule the function to run every 2 seconds rather than having a while loop with a time.sleep. While loops and sleep will lead to your application being unresponsive. – scotty3785 Feb 07 '22 at 10:50
  • @scotty3785 I will try it later. Thank you very much. May I know how to use `.after` for looping? I close my root window by `root.withdraw()` so I can't use `root.after()` to loop – Sorry for my bad English Feb 07 '22 at 11:56
  • Using withdraw doesn't prevent you from using after. Withdraw just hides the GUI, it doesn't destroy it. There are hundreds of online articles explaining how to use after, try this one first https://stackoverflow.com/questions/25753632/tkinter-how-to-use-after-method – scotty3785 Feb 07 '22 at 12:04
  • I fixed it. Thanks. May I know where can I download winico for tk? – Sorry for my bad English Feb 08 '22 at 09:23
  • It seems to be available here https://wiki.tcl-lang.org/page/winico – scotty3785 Feb 08 '22 at 09:26
  • The repo is not found. https://imgur.com/a/mueYn34 – Sorry for my bad English Feb 08 '22 at 11:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241818/discussion-between-sorry-for-my-bad-english-and-scotty3785). – Sorry for my bad English Feb 08 '22 at 11:44

0 Answers0