Home > Back-end >  how to use app.root.current in kivy from main file
how to use app.root.current in kivy from main file

Time:04-02

I want to use 'app.root.current' to shift to another window after our login details are verified. I want to implement it in main file of python. Here is the code for the same: Main file:

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

class SigninWindow(Screen):
    def signin(self):
        db=open('QuizLoginData.txt','r')
        usernam=self.ids.username.text
        passw=self.ids.passw0.text
        popup=Factory.MyPopup()
        una=[]
        pas=[]
        for i in db:
            a,b=i.split(', ')
            b=b.strip()
            una.append(a)
            pas.append(b)
        data=dict(zip(una,pas))
        if usernam in data:
            if passw==data[usernam]:
                popup.ids.message.text = '''Login successful'''
                popup.open()
                app.root.current='quiz' #i want to switch screens after the requirement is satisfied
            else:
                popup.ids.message.text = '''Password Doesn't match'''
                popup.open()
                self.ids.passw0.text = ''
        else:
            popup.ids.message.text = '''Username doesn't exist'''
            popup.open()
            self.ids.username.text = ''
kv = Builder.load_file('loginLayoutCSS.kv')

class Login(App):
    def build(self):
        return kv

if __name__=='__main__':
    Login().run()

Code for kivy file:

QuizScreenManager:
    SigninWindow:
    QuizWindow:

<SigninWindow>:
    name:'signin'
    BoxLayout:
        orientation:'vertical'
        padding:20
        spacing:15
        

        Label:
            text:'Login'
            id:loginHead
            font_size:32
            pos_hint:{'center_x':0.5}

        GridLayout:
            rols:2
            cols:2
            spacing:10

            Label:
                text:'Username'
            TextInput:
                multiline:False
                id:username
                hint_text:'username'
            Label:
                text:'Password'
            PassTextInput:
                multiline:False
                id:passw0
                hint_text:'password'

        SignButton:
            text:'Sign in'
            id:signin
            on_release:
                root.signin()
                # (point - 1)app.root.current='quiz' #it should only work after certain conditions are met
<QuizWindow>:
    name:'quiz'
    Button:
        text:'Start'

In point - 1 i want it to work after the login credentials are verified. I am stuck in this thing can anyone please help in finding the method for this to work. It will be highly appreciated. Thanks in advance

CodePudding user response:

It's hard to guess the actual problem without recreating it. Nonetheless based on your query:

I want to use 'app.root.current' to shift to another window after our login details are verified. I want to implement it in main file of python...

and the fact that you want to do it from a Screen, I believe the following change should work,

Replace app.root.current='quiz' with self.manager.current='quiz'.

  • Related