📜  如何从 python 文件中更改 kivy lang 中的 kivy 按钮文本 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:09.861000             🧑  作者: Mango

代码示例1
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()