1
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
import psutil

battery = psutil.sensors_battery()
percent = str(battery.percent)

class Application(App):
    def build(self):
        return Label(text = percent + "%")


while True:
    Application().run()

The label does not change, even though the percent variable has changed. Though the computer shows the battery level 60%, the app shows the battery level of when the App started.

VRV
  • 29
  • 6

1 Answers1

1

You can use clock.schedule_interval to schedule regular updates. Here is a modified version of your code that does that:

from kivy.app import App
from kivy.clock import Clock
from kivy.uix.label import Label
import psutil

class Application(App):
    def build(self):
        Clock.schedule_interval(self.update, 2)
        return Label(text="Unknown")

    def update(self, dt):
        percent = str(psutil.sensors_battery().percent)
        self.root.text = percent + "%"


if __name__ == '__main__':
    Application().run()
John Anderson
  • 35,991
  • 4
  • 13
  • 36