Python KivyMD:AttributeError:&x27;MDScreen';对象没有属性';添加数据

Python KivyMD:AttributeError:&x27;MDScreen';对象没有属性';添加数据,python,sqlite,kivy,kivy-language,kivymd,Python,Sqlite,Kivy,Kivy Language,Kivymd,AttributeError:“MDScreen”对象没有“添加数据”属性 我是Kivy的新手,正在学习开发应用程序。我试图通过一个按钮使用sqlite从用户的textinput保存数据。我已经在下面发布了python文件和.kv文件 该程序运行良好,但按下提交按钮后,我得到一个AttributeError。我不确定这是否与等级制度有关 from kivymd.app import MDApp import sqlite3 as sql from kivy.uix.screenmanager i

AttributeError:“MDScreen”对象没有“添加数据”属性

我是Kivy的新手,正在学习开发应用程序。我试图通过一个按钮使用sqlite从用户的textinput保存数据。我已经在下面发布了python文件和.kv文件

该程序运行良好,但按下提交按钮后,我得到一个AttributeError。我不确定这是否与等级制度有关

from kivymd.app import MDApp
import sqlite3 as sql
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty


class Start(Screen):
    date = ObjectProperty()
    name = ObjectProperty()
    head = ObjectProperty()
    amount = ObjectProperty()

    def add_data(self):
        con = sql.connect('data.db')
        cur = con.cursor()
        cur.execute(""" INSERT INTO id (date,name,head,amount) VALUES (?,?,?,?)""",
                    (self.date.text, self.name.text, self.head.text, self.amount.text)
                    )
        con.commit()
        con.close()


class Manager(ScreenManager):
    pass


class BTApp(MDApp):
    try:

        con = sql.connect('data.db')
        cur = con.cursor()
        cur.execute(""" CREATE TABLE id(
            date text,
            name text,
            head text,
            amount text)
            """)
        con.commit()
        con.close()
    except:
        pass


if __name__ == '__main__':
    BTApp().run()
以下是在.kv文件中写入的行

MDCard:
    size_hint:None,None
    pos_hint: {"center_x": .5, "center_y": .5}
    md_bg_color: [225/255,59/255,51/255,1]
    size: "280dp", "480dp"
    elevation: 15
    padding: 20
    spacing: 30
    orientation: "vertical"

    MDLabel:
        text:"Deposit"
        halign: "center"
        theme_text_color: "Custom"
        text_color: 1, 1, 1, 1

    MDTextField:
        hint_text: "Date"
        id:date
    MDTextField:
        hint_text: "Name"
        id:name
    MDTextField:
        hint_text: "Head"
        id:head
    MDTextField:
        hint_text: "Amount"
        id:amount

    MDRectangleFlatButton:
        text: "Submit"
        text_color: 1, 1, 1, 1
        md_bg_color: 1, 1, 1, 0
        width:500
        halign: "center"
        on_press: root.add_data()

按:root.add_data()上的
行调用出现该行的
kv
规则的根的
add_data()
方法。奇怪的是,在您发布的
kv
文件中,这将调用
MDCard
上的
add_data()
,而不是
MDScreen