Kivy 应用程序标签未更新,但 Psutil 模块正在更新
Kivy App label not updating but Psutil module are updating
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()
即使百分比变量已更改,标签也不会更改。虽然电脑显示电量60%,但APP显示的是APP启动时的电量。
您可以使用 clock.schedule_interval 安排定期更新。这是您的代码的修改版本:
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()
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()
即使百分比变量已更改,标签也不会更改。虽然电脑显示电量60%,但APP显示的是APP启动时的电量。
您可以使用 clock.schedule_interval 安排定期更新。这是您的代码的修改版本:
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()