简体   繁体   中英

Kivy: Transfer information between screens

I have a simple app where users have to type in username and password to access second screen. To do this I use 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: '  

by using 'show_info()' method I want to display user's information on 'UserInfoPage' screen. To do this I need information from variable 'records' from 'findAccount' account method. I tried to do like this:

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)

but it throws an error because 'findAccount()' method is linked to first screen. How to save information I got on first screen and transfer it on to second screen? Thank you.

First of all, we must correct certain errors:

  • It is not necessary to call findAccount() in the .kv because verify() is calling it in the .py .

  • The LoginPage.findAccount(self) statement is incorrect, you must use self directly to call it, ie the correct thing is self.findAccount() .

  • Avoid using try and except if it is unnecessary, in your case it would only be necessary to verify that records is not None .

To add the information is not necessary to create a new method, a better approach would be to create properties and this is assigned through the screen, to get the appropriate screen we use the get_screen() method of 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM