Change in button text with Kivy language

Asked

Viewed 103 times

-2

I can’t find a way to keep changing the button text every time I click it. I can change the text only once. Every time it is clicked I want to appear on lamp/off lamp.

[![][1]]2

1 answer

0

You would need an auxiliary variable with a boolean value to switch between on and off. See this example below:

class MyButton(Button):
    __status = False

    def change(self):
        self.__status = not self.__status
        self.text = "Desligar" if self.__status else "Ligar"

class App(App):

    def build(self):

        button = MyButton(text = "Ligar", font_size = 40)
        button.on_release = button.change
        return button

In the above example, the attribute __status alternate between True and False at each call of the method change() and then the button text is set according to the value of that attribute.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.