from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty
from kivy.clock import Clock
class MyWidget(Widget):
score = NumericProperty(0)
class MyWindow(Widget):
def update(self, dt):
if self.ids.mywidget.score < 10:
self.ids.mywidget.score += 1
class MyApp(App):
def build(self):
myapp = MyWindow()
Clock.schedule_interval(myapp.update, .25)
return myapp
MyApp().run()
kv:
<MyWindow>:
MyWidget:
id: mywidget
Label:
id: myLabel
text: '0'
font_size: 70
center_x: root.width / 2
center_y: root.height / 2
What do I have to change in my python file to make the application behave the same as if my kv-file was actually this:
<MyWindow>:
MyWidget:
id: mywidget
Label:
id: myLabel
text: str(root.ids.mywidget.score)
font_size: 70
center_x: root.width / 2
center_y: root.height / 2
(I want the label-text to change whenever the score-property changes.)
Thanks in advance
In your MyWidget
class, add a method like:
def on_score(self, *args):
self.parent.ids.myLabel.text = str(self.score)
The above method will be called whenever the score
property changes, and will adjust the myLabel
accordingly.