如何使用if语句从一页重定向到另外两个页面

问题描述

我想让这个程序从数据库中读取信息,如果正确,请转到程序的主页 否则说信息有误

如何使用 if 语句从一个页面重定向到另外两个页面 如果用户输入和密码为 True 通过按下按钮返回主页 否则打印密码或用户错误

enter image description here

应该是从数据库中读取数据

.py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.lang import Builder
from kivy import Config


Builder.load_file("kivy.kv")

class AddUser(Screen):
    def changeScreen(self):
        self.ids.Gotologin.on_press = self.manager.transition.direction = 'up'
    def getUserInfo(self):
        name = self.ids.addusername.text
        paswd = self.ids.addpassword.text
        phone = self.ids.addphone.text
        gmail = self.ids.addgmial.text
        if len(paswd) == 0 and len(name) == 0:
            self.ids.passwordles.text = "Enter your name & Password !"
        elif len(paswd) <= 0:
            self.ids.passwordles.text = "Add a Pssword !"
        elif len(paswd) <= 6:
            self.ids.passwordles.text = "Password is too small"
        elif len(name) <= 0:
            self.ids.passwordles.text = "Enter your Name !"


class Home(Screen):

    def add(self):
        name = self.ids.Name.text
        number = self.ids.Number.text
        ID = self.ids.Id.text
        Buy = self.ids.Buy.text
        sale = self.ids.Sale.text
        ex_date = self.ids.exdate.text
        usage = self.ids.Usage.text
        medicine = self.ids.Medicine.text
        Category = self.ids.Catigory.text


class LogIn(Screen):
    def get(self):
        cr.execute("SELECT * FROM login")
        log = cr.fetchall()
        user = self.ids.username.text
        paswd = self.ids.password.text

Screen_Manager = ScreenManager()

Screen_Manager.add_widget(AddUser(name="Adduser"))
Screen_Manager.add_widget(LogIn(name="Login"))
Screen_Manager.add_widget(Home(name="Home"))

# main class to run application
class MainApp(App):
    def build(self):
        return Screen_Manager

if __name__ == "__main__":
    MainApp().run()

.kv

<AddUser>:
    BoxLayout:
        canvas.before:
            Color:
                rgba:0,1,1
            Rectangle:
                size:self.size
                pos:self.pos
        orientation:"vertical"
        BoxLayout:
            Label:
                id:passwordles
                text:""
        BoxLayout:
            orientation:'vertical'
            BoxLayout:
                Label:
                    size_hint_x:.22
                    text:""
                Label: 
                    text:"User Name *"
                Label:
                    text:"Password *"
                Label:
                    text:"Phone Number"
                Label:
                    text:"Gmail"
                Label:
                    size_hint_x:.22
            BoxLayout:
                Label:
                    size_hint_x:.22
                    text:""
                TextInput:
                    id:addusername
                    size_hint_y:.5
                    hint_text:"User Name"
                TextInput:
                    id:addpassword
                    size_hint_y:.5
                    password:True
                    hint_text:"Password"
                TextInput:
                    id:addphone
                    size_hint_y:.5
                    hint_text:"Phone Number"
                TextInput:
                    id:addgmial
                    size_hint_y:.5
                    hint_text:"Gmail"
                Label:
                    size_hint_x:.22
            BoxLayout:
                Label:
                    text:""
                Button:
                    id:Gotologin
                    radius: 100,100,100
                    font_name:"fonts/MTCORSVA.TTF"
                    color:1,1
                    outline:0,1
                    outline_width:4
                    font_size:32
                    bolde:True
                    size_hint_y:.8
                    text:"Login"
                    on_press:
                        root.getUserInfo()
                        root.manager.transition.direction = 'up'
                        root.manager.transition.duration = .9
                        root.manager.current = 'Login'
                Label:
                    text:""
        BoxLayout:
            Label:
                text:""
<LogIn>:
    BoxLayout:
        orientation:'vertical'
        canvas.before:
            Color:
                rgba:0,1
            Rectangle:
                size:self.size
                pos:self.pos
        BoxLayout:
            Label:
                text:""
        BoxLayout:
            orientation:'horizontal'
            BoxLayout:
                size_hint_x:.4
                Label:
                    text:""
            BoxLayout:
                orientation:"vertical"
                TextInput:
                    id:username
                    size_hint_y:.3
                    hint_text:"User Name"
                TextInput:
                    id:password
                    size_hint_y:.3
                    hint_text:"Password"
                BoxLayout:
                    orientation:"horizontal"
                    Label:
                        size_hint_x:.3
                        text:""
                    Button:
                        id:LoginFromButton
                        on_press:
                            root.manager.transition.direction='left'
                            root.manager.transition.duration = .9
                            root.manager.current = 'Home'
                        text:'Login'
                        font_name:"fonts/MTCORSVA.TTF"
                        color:1,1
                        outline:0,1
                        outline_width:4
                        font_size:32
                        bolde:True
                        size_hint_y:.5
                    Label:
                        size_hint_x:.3
                        text:""
            BoxLayout:
                size_hint_x:.4
                Label:
                    text:""

        BoxLayout:
            Label:
                text:""

<Home>:
    Button:
        text:"Home"

解决方法

在您的 kv 中,将 on_press 设置为一个方法,如下所示:

                Button:
                    id:LoginFromButton
                    on_press:
                        root.do_login()

然后,在 LogIn 屏幕类中:

def check_login(self):
    # do your check for legal login and return True or False
    return True

def do_login(self):
    if self.check_login():
        self.manager.current = 'Home'