我试图将微调器的值保存到文件中,但出现此错误:AttributeError:'NoneType'对象没有属性'text'

时间:2019-07-16 16:02:52

标签: kivy spyder kivy-language

我正在尝试开发一个使用通用充电器为某些电动自行车充电的应用程序。因此,用户需要一个帐户,在注册时,他们从微调器中选择了自己拥有的电动自行车。该值需要存储在用户文件中,以便以后使用。问题是,当我尝试保存帐户时,出现以下错误:

AttributeError: 'NoneType' object has no attribute 'text'

当我删除所有保存微调器值的程序时,该程序将正常运行。 有人可以帮我解决这个问题吗?

主应用程序ULiVEmain.py的一部分

class CreateAccountWindow(Screen):      #create an account
    namee = ObjectProperty(None)
    email = ObjectProperty(None)
    password = ObjectProperty(None)
    bikebrand = ObjectProperty(None)

    def submit(self):
        if self.namee.text != "" and self.email.text != "" and self.email.text.count("@") == 1 and self.email.text.count(".") > 0:
            if self.password != "":
                db.add_user(self.email.text, self.password.text, self.namee.text, self.bikebrand.text)

                self.reset()
                sm.current = "login_screen"
            else:
                invalidForm()
        else:
            invalidForm()

    def login(self):
        self.reset()
        sm.current = "login_screen"

    def reset(self):
        self.email.text = ""
        self.password.text = ""
        self.namee.text = ""

    def spinnerBrand(self, text):
        print("Brand of the bike is: " + text)

kv = Builder.load_file("LayoutULiVEApp.kv")

sm = WindowManager()
db = DataBase("users.txt")

用于保存帐户数据的py文件:database.py

import datetime

class DataBase:
    def __init__(self, filename):
        self.filename = filename
        self.users = None
        self.file = None
        self.load()

    def load(self):
        self.file = open(self.filename, "r")
        self.users = {}

        for line in self.file:
            email, password, name, created, bikebrand= line.strip().split(";")
            self.users[email] = (password, name, created, bikebrand)

        self.file.close()

    def get_user(self, email):
        if email in self.users:
            return self.users[email]
        else:
            return -1

    def add_user(self, email, password, name, bikebrand):
        if email.strip() not in self.users:
            self.users[email.strip()] = (password.strip(), name.strip(), bikebrand.strip(), DataBase.get_date())
            self.save()
            return 1
        else:
            print("Email exists already")
            return -1

    def validate(self, email, password):
        if self.get_user(email) != -1:
            return self.users[email][0] == password
        else:
            return False

    def save(self):
        with open(self.filename, "w") as f:
            for user in self.users:
                f.write(user + ";" + self.users[user][0] + ";" + self.users[user][1] + ";" + self.users[user][2] + ";" +self.users[user][3] + "\n")

    @staticmethod
    def get_date():
        return str(datetime.datetime.now()).split(" ")[0]

kv文件的一部分,以创建一个帐户

<CreateAccountWindow>
    name: "create"

    namee: namee
    email: email
    password: passw

    FloatLayout:
        cols:1

        FloatLayout:
            size: root.width, root.height/2

            Label:
                text: "Create an Account"
                size_hint: 0.8, 0.2
                pos_hint: {"x":0.1, "top":1}
                font_size: (root.width**2 + root.height**2) / 14**4

            Label:
                size_hint: 0.26,0.15
                pos_hint: {"x":0, "top":0.8}
                text: "Name: "
                font_size: (root.width**2 + root.height**2) / 14**4

            TextInput:
                pos_hint: {"x":0.3, "top":0.76}
                size_hint: 0.5, 0.08
                id: namee
                multiline: False
                font_size: (root.width**2 + root.height**2) / 14**4

            Label:
                size_hint: 0.26,0.15
                pos_hint: {"x":0, "top":0.7}
                text: "Email: "
                font_size: (root.width**2 + root.height**2) / 14**4

            TextInput:
                pos_hint: {"x":0.3, "top":0.66}
                size_hint: 0.5, 0.08
                id: email
                multiline: False
                font_size: (root.width**2 + root.height**2) / 14**4

            Label:
                size_hint: 0.2,0.15
                pos_hint: {"x":0, "top":0.6}
                text: "Password: "
                font_size: (root.width**2 + root.height**2) / 14**4

            TextInput:
                pos_hint: {"x":0.3, "top":0.56}
                size_hint: 0.5, 0.08
                id: passw
                multiline: False
                password: True
                font_size: (root.width**2 + root.height**2) / 14**4

            Label:
                size_hint: 0.26,0.15
                pos_hint: {"x":0, "top":0.5}
                text: "Bike: "
                font_size: (root.width**2 + root.height**2) / 14**4

            Spinner:
                id: bikebrand
                on_text:
                    root.spinnerBrand(bikebrand.text)   
                text: "<Select>"
                values: ['Brand A', 'Brand B', 'Brand C', 'Brand D', 'Brand E', 'Brand F', 'Brand G', 'Brand H', 'Brand I']
                background_color: 1,1,1,1
                color: 0,0,0,1
                color_down: 0,0,0,1
                size_hint: 0.5, 0.08
                pos_hint: {"x":0.3, "top":0.46}

            Button:
                pos_hint:{"x":0.3,"y":0.25}
                size_hint: 0.5, 0.1
                font_size: (root.width**2 + root.height**2) / 15**4
                text: "Already have an Account? Log In"
                background_color: 0.68, 0.8, 0.5, 0.18
                color: 0,0,0,1
                on_release:
                    root.manager.transition.direction = "left"
                    root.login()

            Button:
                pos_hint:{"x":0.3,"y":0.05}
                size_hint: 0.5, 0.12
                text: "Submit"
                font_size: (root.width**2 + root.height**2) / 13**4
                background_color: 0.68, 0.8, 0.5, 0.18
                color: 0,0,0,1
                on_release:
                    root.manager.transition.direction = "left"
                    root.submit()

1 个答案:

答案 0 :(得分:0)

在您的kv文件中,您尚未定义ObjectProperty bikebrand。我相信您只需要更改:

namee: namee
email: email
password: passw

包括bikebrand

namee: namee
email: email
password: passw
bikebrand: bikebrand