how to change a kivy button text in kivy lang from python file
from kivy.app import App from kivy.lang.builder import Builder from kivy.properties import StringProperty from kivy.uix.boxlayout import BoxLayout class Container(BoxLayout): message = StringProperty() def retranslate(self, language): texts = {"en": "Hello World", "fr": "Salut monde"} self.message = texts.get(language, "") Builder.load_string( """: orientation: 'vertical' Button: text: root.message Button: text: "Eng" on_press: root.retranslate("en") Button: text: "Fra" on_press: root.retranslate("fr") """ ) class MyApp(App): def build(self): w = Container() w.retranslate("en") return w if __name__ == "__main__": MyApp().run()
Here is what the above code is Doing:
1. The Container class is a subclass of BoxLayout. It has a message property that is a StringProperty.
2. The retranslate method takes a language parameter and sets the message property to the appropriate text.
3. The Builder.load_string method is used to create the GUI.
4. The MyApp class is a subclass of App. It creates an instance of Container and calls the retranslate method.
5. The MyApp class is run.