'Updating Created Custom KivyMD Property

I'm hoping there's a way to do this but not entirely sure. I want a way to update all of my labels after a button press. This is how I'm currently doing it which throws an error but I don't quite understand it.

At the moment I'm trying to add an id to the custom label and then call it but that doesn't work.

Python Code:

from kivy.lang import Builder
from kivymd.app import MDApp


class MainApp(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.screen = Builder.load_file("main.kv")

    def build(self):
        self.theme_cls.theme_style = "Dark"

        return self.screen

    def button_press(self):
        self.root.ids.custom_label.text = "Two"


if __name__ == '__main__':
    MainApp().run()

Kivy File:

<MDLabel>
    id: custom_label
    text: "One"
    halign: "center"


BoxLayout:
    orientation: "vertical"

    MDLabel

    MDLabel

    MDRoundFlatButton:
        text: "Press"
        pos_hint: {"center_x": 0.5, "center_y": 0.5}
        on_release: app.button_press()


Solution 1:[1]

First things first you can't name a custom label, MDLabel. restructure your code like so. Starting with your .kv file

<CustomLabel@MDLabel>
    text: "One"
    halign: "center"


BoxLayout:
    orientation: "vertical"

    CustomLabel:
        id: custom_label_1

    CustomLabel:
        id: custom_label_2

    MDRoundFlatButton:
        text: "Press"
        pos_hint: {"center_x": 0.5, "center_y": 0.5}
        on_release: app.button_press()

The in your .py file

from kivy.lang import Builder
from kivymd.app import MDApp


class MainApp(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.screen = Builder.load_file("main.kv")

    def build(self):
        self.theme_cls.theme_style = "Dark"

        return self.screen

    def button_press(self):
        self.root.ids.custom_label_1.text = "Two"
        self.root.ids.custom_label_2.text = "Two"


if __name__ == '__main__':
    MainApp().run()

Hope that works. you can't have 2 widgets using the same id. so if you wanna change the the text of 2 custom labels you gonna have to reference them individually

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 darkmatter08