我有一个简单的应用程序,用户必须输入用户名和密码才能访问第二个屏幕。要做到这一点,我使用sqlite 的 * PY
class LoginPage(Screen):
def findAccount(self):
username = self.ids["login_in"].text
password = self.ids["pass_in"].text
c.execute('''SELECT * FROM customer WHERE name = ? AND password = ?;
''', (username, password))
records = c.fetchone() #returns (1, Bob, qwerty, bob@gmail.com, 071234556)
return records
def verify(self):
try:
records = LoginPage.findAccount(self)
if self.ids["login_in"].text == records[1] and self.ids["pass_in"].text == records[2]:
self.manager.current = "user"
except TypeError:
print("Something is not correct")
class UserInfoPage(Screen):
def show_info(self):
pass
class ScreenManagement(ScreenManager):
pass
kv_file = Builder.load_file("login.kv")
class LoginApp(App):
def build(self):
return kv_file
*的Kv
ScreenManager:
LoginPage:
UserInfoPage:
<LoginPage>:
orientation: "vertical"
name: "login"
BoxLayout:
TextInput:
id: login_in
TextInput:
id: pass_in
password: True
Button:
text: "GO"
on_release:
root.findAccount()
root.verify()
<UserInfoPage>:
name: "user"
BoxLayout:
Label:
id: the_id
text: 'id: '
Label:
text: 'name: '
Label:
text: 'email: '
Label:
text: 'phone number: '
使用&#39; show_info()&#39;方法我想在&#39; UserInfoPage&#39;上显示用户的信息。屏幕。要做到这一点,我需要来自变量&#39;记录的信息。来自&#39; findAccount&#39;帐户方法。我试着这样做:
def show_info(self):
information = LoginPage.findAccount(self)
user_id = information[0]
self.ids["the_id"].text = self.ids["the_id"].text + str(user_id)
但它会引发错误,因为&#39; findAccount()&#39;方法链接到第一个屏幕。如何保存我在第一个屏幕上获得的信息并将其传输到第二个屏幕? 谢谢。
答案 0 :(得分:0)
首先,我们必须纠正某些错误:
没有必要在findAccount()
中致电.kv
,因为verify()
正在.py
中调用它。
LoginPage.findAccount(self)
语句不正确,您必须直接使用self来调用它,即正确的是self.findAccount()
。
避免使用try,除非没有必要,否则在您的情况下,只需验证records
不是None
。
要创建新方法不需要添加信息,更好的方法是创建属性并通过屏幕分配,以获得适当的屏幕,我们使用get_screen()
ScreenManager
方法}}
<强> *。PY 强>
class LoginPage(Screen):
def findAccount(self):
username = self.ids["login_in"].text
password = self.ids["pass_in"].text
c.execute('''SELECT * FROM customer WHERE name = ? AND password = ?;
''', (username, password))
records = c.fetchone() #returns (1, Bob, qwerty, bob@gmail.com, 071234556)
return records
def verify(self):
records = self.findAccount()
if records is not None:
_id, name, password, email, phone_number = records
if self.ids["login_in"].text == name and self.ids["pass_in"].text == password:
user_screen = self.manager.get_screen("user")
user_screen._id = str(_id)
user_screen._name = name
user_screen._email = email
user_screen._phone_number = phone_number
self.manager.current = "user"
else:
print("Something is not correct")
class UserInfoPage(Screen):
pass
class ScreenManagement(ScreenManager):
pass
<强> *。KV 强>
ScreenManager:
LoginPage:
UserInfoPage:
<LoginPage>:
orientation: "vertical"
name: "login"
BoxLayout:
TextInput:
id: login_in
TextInput:
id: pass_in
password: True
Button:
text: "GO"
on_release:
root.verify()
<UserInfoPage>:
name: "user"
_id: ""
_name: ""
_email: ""
_phone_number: ""
BoxLayout:
Label:
text: 'id: ' + root._id
Label:
text: 'name: ' + root._name
Label:
text: 'email: ' + root._email
Label:
text: 'phone number: ' + root._phone_number