0

I have an app but how do i change screen not through the kv, but through the python function, There is my python code and i get an error name 'root' is not defined. Tried to do the same thing but with self and it said that MainApp has no attribute screen_manager, so how do i access it?

class Main(MDBoxLayout):
    pass
class LoginScreen(Screen):
    pass
class MainApp(MDApp):
    def build(self):
        return Builder.load_file("Main.kv")
    def on_start(self):
        if is_logged():
            self.screen_manager.current="main_screen"
        else:
            self.screen_manager.current="login_screen"

    def is_logged():
        return False

MainApp().run()

also here is my kv code

<LoginScreen>:
    id: login_screen
    text: "Login"
    icon: "login"

    MDBoxLayout:
        orientation: "vertical"
        MDLabel:
            text:"Welcome to Plaim"
            font_style:"H4"
            text_color:"0,0,0,1"          
        MDTextField:
            hint_text:"login"
            id: username
            hint_text: "Username"
            required: True
            helper_text_mode: "on_error"
        MDTextField:
            hint_text:"password"
            id: password
            password: True
            hint_text: "Password"
            required: True
            helper_text_mode: "on_error"

        MDRaisedButton:
            text: "Sign In"
            on_release:
                app.change_screen("main_screen")
<Main>:
    orientation: "vertical"
    id: main
    ScreenManager:
        LoginScreen:
            name: "login_screen"
            id: login_screen
        MainScreen:
            name: "main_screen"
            id: main_screen
BogdanTsyn
  • 41
  • 6
  • you could create minimal working code with all imports. – furas Feb 05 '21 at 23:08
  • if `is_logged()` is part of class `MainApp()` then you need `self` - `def is_logged(self):` and `if self.is_logged()` – furas Feb 05 '21 at 23:10
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful informatin. – furas Feb 05 '21 at 23:10
  • I tried to run your code and in some moments worked for me `self.root.current` – furas Feb 06 '21 at 00:23
  • or `self.root.ids.screen_manager.current` but it needs `ScreenManager: id: screen_manager` in `kv` file. – furas Feb 06 '21 at 00:30

1 Answers1

0

I created minimal working code and I needed

self.root.current = "main_screen"

or

self.root.ids.screen_manager.current = "main_screen"

main.py

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen #, ScreenManager
from kivymd.uix.boxlayout import MDBoxLayout

class Main(MDBoxLayout):
    pass
    
class LoginScreen(Screen):
    pass

class MainScreen(Screen):
    pass
    
class MainApp(MDApp):

    def build(self):
        return Builder.load_file("Main.kv")
        
    def on_start(self):
        #print(type(self))
        #print(type(self.root))
        #print(type(self.root.ids))
        #print(type(self.root.ids.screen_manager))

        if is_logged():
            self.root.current = "main_screen"
            #self.root.ids.screen_manager.current = "main_screen"
        else:
            self.root.current = "login_screen"
            #self.root.ids.screen_manager.current = "login_screen"

def is_logged():
    return True

MainApp().run()

Main.kv

Main:
    id: main
    name: "main"
    Button:
        text: "Hello World"
        
    ScreenManager:
        id: screen_manager
        name: "screen_manager"
        
        LoginScreen:
        MainScreen:

<LoginScreen>:
    id: login_screen
    name: "login_screen"
    
    text: "Login"
    icon: "login"
    MDBoxLayout:
        orientation: "vertical"
        MDLabel:
            text:"Welcome to Plaim"
            font_style:"H4"
            text_color:"0,0,0,1"          
        MDTextField:
            hint_text:"login"
            id: username
            hint_text: "Username"
            required: True
            helper_text_mode: "on_error"
        MDTextField:
            hint_text:"password"
            id: password
            password: True
            hint_text: "Password"
            required: True
            helper_text_mode: "on_error"

        MDRaisedButton:
            text: "Sign In"
            on_release:
                root.manager.current = "main_screen"
                root.manager.transition.direction = "left"
                
<MainScreen>:
    id: main_screen
    name: "main_screen"
    
    orientation: "vertical"
    Button:
        text: "LOGIN"
        on_release:
            root.manager.current = "login_screen"
            root.manager.transition.direction = "right"

BTW:

If I use lower case name main.kv then I don't need

 return Builder.load_file("Main.kv")

but I can use

 return Main()

and it loads main.kv automatically.

But in main.kv I have to use <Main>: instead of Main:


main.py

from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen #, ScreenManager
from kivymd.uix.boxlayout import MDBoxLayout

class Main(MDBoxLayout):
    pass
    
class LoginScreen(Screen):
    pass

class MainScreen(Screen):
    pass
    
class MainApp(MDApp):

    def build(self):
        return Main()
        
    def on_start(self):
        #print(type(self))
        #print(type(self.root))
        #print(type(self.root.ids))
        #print(type(self.root.ids.screen_manager))

        if is_logged():
            self.root.current = "main_screen"
            #self.root.ids.screen_manager.current = "main_screen"
        else:
            self.root.current = "login_screen"
            #self.root.ids.screen_manager.current = "login_screen"

def is_logged():
    return True

MainApp().run()

main.kv (lower case name)

<Main>:
    id: main
    name: "main"
    Button:
        text: "Hello World"
        
    ScreenManager:
        id: screen_manager
        name: "screen_manager"
        
        LoginScreen:
        MainScreen:

<LoginScreen>:
    id: login_screen
    name: "login_screen"
    
    text: "Login"
    icon: "login"
    MDBoxLayout:
        orientation: "vertical"
        MDLabel:
            text:"Welcome to Plaim"
            font_style:"H4"
            text_color:"0,0,0,1"          
        MDTextField:
            hint_text:"login"
            id: username
            hint_text: "Username"
            required: True
            helper_text_mode: "on_error"
        MDTextField:
            hint_text:"password"
            id: password
            password: True
            hint_text: "Password"
            required: True
            helper_text_mode: "on_error"

        MDRaisedButton:
            text: "Sign In"
            on_release:
                root.manager.current = "main_screen"
                root.manager.transition.direction = "left"
                
<MainScreen>:
    id: main_screen
    name: "main_screen"
    
    orientation: "vertical"
    Button:
        text: "LOGIN"
        on_release:
            root.manager.current = "login_screen"
            root.manager.transition.direction = "right"
furas
  • 134,197
  • 12
  • 106
  • 148