在 Kivy 中创建一个表单来填充数据库

Creating a form to populate a database in Kivy

最终目标是使用各种小部件(文本输入、复选框等)使用表单中的信息在数据库中创建条目。我想在表单末尾制作一个按钮来提交对命令的回复,但我被卡住了。我能想到的唯一方法是将所有 id 设为对象属性,然后在像 series1.value...series2.value...

这样的函数中单独引用它们

这看起来很麻烦,以后很难在表单中添加更多条目。

这是我的.py:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.checkbox import CheckBox
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.button import Button


class DermRoot(BoxLayout):
    dict_filled_from_form = {}
    def add_entry_to_db(self,dict_filled_from_form):
        pass

class DemographForm(BoxLayout):
    sex = ObjectProperty()
    age_input = ObjectProperty()

    def add_entry(self,dict_of_form):
        dict_filled_from_form = dict_of_form
        #is this possible?
        #or do I have to do dict_filled_from_form['series1'] = series1.value etc

class DermApp(App):
    pass

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

和我的 kv 文件

DermRoot:

<DermRoot>:
    DemographForm

<DemographForm>:
    orientation: "vertical"

    GridLayout:
        age_input: age_input
        sex: sex

        cols: 2
        Label:
            text: "Patient's Age:"
        TextInput:
            id: age_input
            focus: series4
            multiline: False
        Label:
            text: "Sex:"
        ToggleButton:
            id: sex 
            text: "Male" if self.state == 'normal' else "Female"
        Label:
            text: "Standard Series to Include?"
        GridLayout:
            series1: series1
            series2: series2
            series3: series3 
            series4: series4
            series5: series5

            cols: 2
            CheckBox:
                id: series1
            Label:
                text: "Series1"
            CheckBox:
                id: series2
            Label:
                text: "Series2"
            CheckBox:
                id: series3
            Label:
                text: "series"
            CheckBox:
                id: series4
            Label:
                text: "series4"
            CheckBox:
                id: series5
            Label:
                text: "Series 5"
    Button:
        height: "40dp"
        size_hint_y: None
        text: "Add Entry to Database"
        on_press: root.add_entry(dict_of_form = 'how do I get this?')

最好的选择是使用 DictProperty。尝试将 1 添加到 DemographForm:

class DemographForm(BoxLayout):
    data = DictProperty({})

在你的kv中,你可以这样使用它:

    Label:
        text: "Patient's Age:"
    TextInput:
        multiline: False
        # set the value from the data dict. if the key
        # doesn't exist in the dict, then use a default value
        text: root.data['age'] if 'age' in root.data else ''
        # when the value changes, update the data dict
        on_text: root.data['age'] = self.text

这也使得以后处理编辑记录变得更加容易。如果将新字典设置为 data,它将填充表单中的所有字段。让我们添加一个 edit() 方法到 DemographForm:

    def edit(self, data=None):
        if data is not None:
            self.data = data
        else:
            self.data = {}  # reset the form

因此您可以填充或重置表单。你如何取回数据?有不同的方法可以做到这一点,但让我们使用一个事件。在 DemographForm 中,添加一个 __events__ 属性和一个默认处理程序:

    __events__ = ('on_save', )

    def on_save(self, data):
        pass

默认处理程序无需执行任何操作。如果您创建一个事件,您只需要定义它。接下来我们可以将它连接到 kv:

中的保存按钮
    Button:
        height: "40dp"
        size_hint_y: None
        text: "Add Entry to Database"
        # dispatch our new `on_save` event
        on_press: root.dispatch('on_save', root.data)

现在你有了一个 on_save 事件,你可以用同样的方式绑定到你的 kv 中:

<DermRoot>:
    DemographForm:
        # the arguments to the handler are available
        # as `args` - the last argument will be our
        # data, so we can grab it via `args[-1]`
        on_save: root.add_entry_to_db(args[-1])