i use tkinter, in one part of my code , i show battery percentage on screen and i use psutil module,
charging = tk.Label(DOWN_LEFT,bg='#0000CD',font=('Acumin Variable Concept',45,'bold '))
charging.place(x=420,y=60)
def battery():
battery = psutil.sensors_battery()
charging.config(text=f'{battery.percent}%')
battery()
everything is ok, until the battery percentage increases or decreses, but my value on the tkniter screen does not. do you have any idea???
CodePudding user response:
Use root.after(ms, func)
function and make sure you have separate names for the function battery
and the variable battery
. For now the variable is b. The Interval is 1000 in the root.after
function meaning that it checks every second. You can change that if you want. It's in milliseconds. This code is an example of what I just mentioned above.
import psutil
root = tk.Tk()
charging = tk.Label(root , text='', bg='#0000CD',font=('Acumin Variable Concept',45,'bold '))
charging.place(x=420,y=60)
def battery():
b = psutil.sensors_battery()
charging.config(text=f'{b.percent}%')
root.after(1000, battery)
battery()
root.mainloop()
Hopefully this works for you.