i'm kindda newbie in python, trying to practice,
after hours of examinations i understood the problem with my program is with the win10toast.
it seems having someproblems with tkinter window.
# -*- coding: utf-8 -*-
"""
Created on Sun May 24 18:18:00 2020
@author: MeTaNa
"""
'''
this program is simple, notifys u if battery is fully charged,
'''
from tkinter import Label, Tk, Button
import os
import psutil
import win10toast
# current_statues = percent + '% | ' + plugged
def preload():
pass
def start():
global percent
global plugged
battery = psutil.sensors_battery()
plugged = battery.power_plugged
percent = str(battery.percent)
if plugged == False:
plugged = "Not Plugged In"
else:
plugged = "Plugged In"
if (psutil.sensors_battery().power_plugged == True) and (battery.percent == 100):
print(percent + '% | ' + plugged)
print('Unplug the Charger Please!')
win10toast.ToastNotifier().show_toast('Battery Statues', 'Battery Full.\nUnplug the Charger Please!', icon_path='', duration=10)
gui.after(5000, start)
elif (psutil.sensors_battery().power_plugged == False) and (battery.percent != 100):
print(percent + '% | ' + plugged)
print('Not Charging...')
win10toast.ToastNotifier().show_toast('Battery Statues', 'Charger Not Plugged', icon_path='', duration=10)
gui.after(5000, start)
else:
print(percent + '% | ' + plugged)
print('Charging...')
win10toast.ToastNotifier().show_toast('Battery Statues', 'Charging...', icon_path='', duration=10)
gui.after(5000, start)
def closer():
try:
os.system('TASKKILL /F /IM Bat2.exe')
except Exception:
print('already closed!')
gui.destroy()
# create a GUI window
gui = Tk()
gui.resizable(False, False)
gui.geometry("200x200")
gui.configure(background="white")
gui.title("Battery Notifier")
battey_statues = Label(gui, text='percent')
battey_statues.pack()
button1 = Button(gui, text=' Start Script ', fg='white', bg='gray', command=start, height=2, width=9)
button1.pack()
button2 = Button(gui, text=' Stop Script ', fg='white', bg='gray', command=closer, height=2, width=9)
button2.pack()
# start the GUI
gui.after(100, preload)
gui.mainloop()
when i press the start script button, it shall call the start function, but it freezes the tkinter window, i don't understand why.
previously i had a problem with tkinter and while loop, i solved with after method.
- the script runs correctly without tkinter.
so, any ideas what shall i do?