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"