Kivy 如何从 kivy 中的字典创建小部件

Kivy How to create widgets from dictionary in kivy

我是 Python 图书馆 kivy 的新手。我找到了额外的库 kivy-md,它有很好看的 ui 元素。目前我想从字典变量创建许多 MDTextField 小部件,例如

# text_fields.py
text_fields = {
    "text_field1": {
        "key": 0,
        "self": "text_field1",
        "hint_text": "Textfield 1",
        "helper_text": "This is textfield 1",
    },

    "text_field2": {
        "key": 1,
        "self": "text_field2",
        "hint_text": "Textfield 2",
        "helper_text": "This is textfield 2",
    }
}

根据这个变量,我想在 kv 文件中创建看起来像

的小部件
GridLayout:
    MDTextField:
        id: text_field1
        hint_text: text_fields["text_field1"]["hint_text"]
        helper_text: text_fields["text_field1"]["helper_text"]
        helper_text_mode: "on_focus"
        on_text_validate: app.on_text_validate(text_fields["text_field1"]["self"])
    MDTextField:
        id: text_field2
        hint_text: text_fields["text_field2"]["hint_text"]
        helper_text: text_fields["text_field2"]["helper_text"]
        helper_text_mode: "on_focus"
        on_text_validate: app.on_text_validate(text_fields["text_field2"]["self"])

我发现这个问题有两个我无法解决的挑战。第一个是如何解析字典字符串中的 id 值,第二个问题是我不知道如何循环 kv 文件中的 kivy 小部件。

有没有办法实现我想要的?

请注意,KivyMD MDTextField 中没有属性 selfkey。在例子中,我们使用嵌套的for循环来访问字典,text_fields.py

例子

main.py

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivymd.textfields import MDTextField
from kivymd.theming import ThemeManager
from text_fields import text_fields
from functools import partial


class RootWidget(GridLayout):

    def __init__(self, **kwargs):
        super(RootWidget, self).__init__(**kwargs)
        self.cols = 1
        app = App.get_running_app()
        for key, fields in text_fields.items():
            mdt = MDTextField(id=key, helper_text_mode="on_focus")
            for subfield, value in fields.items():
                mdt.subfield = str(value)
            mdt.bind(on_text_validate=partial(app.on_text_validate, text_fields[key]["self"]))
            self.add_widget(mdt)


class MainApp(App):
    title = "KivyMD MDTextField Demo"
    theme_cls = ThemeManager()

    def build(self):
        return RootWidget()

    def on_text_validate(self, value, obj):
        print("\napp.on_text_validate:")
        print("\tobj=", obj)
        print("\tvalue=", value)
        print("\tobj.text=", obj.text)


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

输出