Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
question from:https://stackoverflow.com/questions/66068943/how-do-i-change-screen-through-the-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
87 views
Welcome To Ask or Share your Answers For Others

1 Answer

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...