'Using both MDBottomNavigation & ScreenManager?
So I'm new to kivy & kivyMD and am trying to figure out how to switch between screens. I want to use the MDBottomNavigation to switch between screens, but I also have other screens in my app that will be accessed using buttons. I'm trying to figure out how to incorporate the MDBottomNavigation with the ScreenManager. What I did was have multiple MDBottomNavigation, one for each screen. Then I would have buttons within those screens that will be used to get to other screens.
My python file:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp
from kivy.utils import get_color_from_hex
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class ThirdWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("my.kv")
class MyMainApp(MDApp):
def build(self):
return kv
if __name__ == "__main__":
MyMainApp().run()
My kv file:
#: import get_color_from_hex kivy.utils.get_color_from_hex
WindowManager:
MainWindow:
SecondWindow:
ThirdWindow:
<MainWindow>:
MDBottomNavigation:
panel_color: get_color_from_hex("#02AD9C")
MDBottomNavigationItem:
name: 'screen 1'
text: 'Scr1 Tab1'
on_enter: root.current = 'MainWindow'
MDLabel:
text: 'Screen_1 Tab_1'
halign: 'center'
MDBottomNavigationItem:
name: 'screen 2'
text: 'Scr1 Tab2'
on_enter: root.current = 'SecondWindow'
MDLabel:
text: 'Screen_1 Tab_2'
halign: 'center'
MDBottomNavigationItem:
name: 'screen 3'
text: 'Scr1 Tab3'
on_enter: root.current = 'ThirdWindow'
MDLabel:
text: 'Screen_1 Tab_3'
halign: 'center'
<SecondWindowWindow>:
MDBottomNavigation:
panel_color: get_color_from_hex("#02AD9C")
MDBottomNavigationItem:
name: 'screen 1'
text: 'Scr2 Tab1'
on_enter: root.current = 'MainWindow'
MDLabel:
text: 'Screen_2 Tab_1'
halign: 'center'
MDBottomNavigationItem:
name: 'screen 2'
text: 'Scr2 Tab2'
on_enter: root.current = 'SecondWindow'
MDLabel:
text: 'Screen_2 Tab_2'
halign: 'center'
MDBottomNavigationItem:
name: 'screen 3'
text: 'Scr3 Tab3'
on_enter: root.current = 'ThirdWindow'
MDLabel:
text: 'Screen_3 Tab_3'
halign: 'center'
<ThirdWindow>:
MDBottomNavigation:
panel_color: get_color_from_hex("#02AD9C")
MDBottomNavigationItem:
name: 'screen 1'
text: 'Scr3 Tab1'
on_enter: root.current = 'MainWindow'
MDLabel:
text: 'Screen_3 Tab_1'
halign: 'center'
MDBottomNavigationItem:
name: 'screen 2'
text: 'Scr3 Tab2'
on_enter: root.current = 'SecondWindow'
MDLabel:
text: 'Screen_3 Tab_2'
halign: 'center'
MDBottomNavigationItem:
name: 'screen 3'
text: 'Scr3 Tab3'
on_enter: root.current = 'ThirdWindow'
MDLabel:
text: 'Screen_3 Tab_3'
halign: 'center'
When I run the code I get an error: ValueError: KivyMD: App object must be initialized before loading root widget
What is this referring to? Is this referring to the screens because I thought they were already initialized?
Does anyone have a solution or perhaps a better way to have both MDBottonNavigation & ScreenManager? Would appreciate any feedback.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
