Issue
I'm trying to make an app to charge some e-bikes with an universal charger. So the user needs an account, at the registration they chose the e-bike they have out of the options in the spinner. This value needs to be stored at a user-file so it can be used later. The problem is when I try to save the account I get the following error:
AttributeError: 'NoneType' object has no attribute 'text'
When I delete everything of saving the values of the spinner the program works perfectly. Can someone help me with this problem?
part of the main app 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-file to save the accountdata: 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]
part of the kv-file, to create an account
<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()
Solution
In your kv
file, you haven't defined the ObjectProperty
bikebrand
. I believe you only need to change:
namee: namee
email: email
password: passw
to include the bikebrand
:
namee: namee
email: email
password: passw
bikebrand: bikebrand
Answered By - John Anderson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.