Search code examples
pythonkivylogin-page

how to redirect from one page two other page with an if statement


I want this program to read information from the database and if it was correct, go to the main page of the program Otherwise say that the information is wrong

how to redirect from one page two other page with an if statement if user input and password was True by pressing button go to home otherwise print password or user wrong

enter image description here

It is supposed to read data from the database

.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,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, 100
                    font_name:"fonts/MTCORSVA.TTF"
                    color:1,1,0,1
                    outline:0,0,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,0,1,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,0,1
                        outline:0,0,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"

Solution

  • In your kv, set the on_press to a method, like this:

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

    Then, in the LogIn screen class:

    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'