Skip to content Skip to sidebar Skip to footer

Kivy: Self-updating Label Text

Let's say I have 3 classes: a 'woking class' where stuff takes place, a label class and a class to contain them. For example the label class could be a status bar showing the stat

Solution 1:

You're updating a property on WorkingClass, but that doesn't update the value on CustomLabel since you did a direct assignment instead of binding it. But yes, you can use Propertys to make everything work automatically.

In WorkingClass:

classWorkingClass(BoxLayout):
    a = NumericProperty()

    def__init__(self, **kwargs): ...

This makes a into a Property which you can bind to.

Then in MainLayout's constructor:

self.workingClass = WorkingClass()
self.customLabel = CustomLabel(value=self.workingClass.a)
self.workingClass.bind(a=self.customLabel.setter('value'))

The last line says: "when the value of property a on self.workingClass changes, set the value property of self.customLabel to the same value"

Alternatively, you could just add the Property to WorkingClass above, then get rid of MainLayout's constructor and use kv instead:

<MainLayout>:orientation:'vertical'WorkingClass:id:working_classCustomLabel:value:working_class.a# assigning one property to another in kv automatically binds

Post a Comment for "Kivy: Self-updating Label Text"