Reputation: 11
I have a seemingly pretty easy question. I am making a app with kivymd and the first .kv file is sort of long. I have three .kv files that I would like to navigate in between. Currently i'm wondering how to use Builder.load_file, or something else to read from window to window, thanks for your help.
Upvotes: 0
Views: 980
Reputation: 1
Not sure if there is a better way to do this or not. I myself couldn't find anything but ended up doing the following.
Create your main .kv file that is a screen manager. Make sure you include your other .kv files with '#:include 'filename.kv'
#:include screen1.kv
#:include screen2.kv
ScreenManager:
Screen1:
Screen2:
Format your other .kv files however you'd like. Just make sure to include a name for referencing in your screen manager. You must also name the screens the same as you did in your main.kv file.
<Screen1>
name: 'screen1'
orientation: 'vertical'
MDLabel:
text: 'Screen1'
halign: 'center'
MDFlatButton:
text: 'Go to screen 2'
pos_hint: {'center_x': 0.5}
on_release: root.manager.current = 'screen2'
This is .kv file #2
<Screen2>
name: 'screen2'
orientation: 'vertical'
MDLabel:
text: 'Screen2'
halign: 'center'
MDFlatButton:
text: 'Go to screen 1'
pos_hint: {'center_x': 0.5}
on_release: root.manager.current = 'screen1'
Your main.py file will have to include the libraries, define the classes, add the screens to your screen manager, and then you can build your main.kv file in your MDApp.
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.lang import Builder
class Screen1(Screen):
pass
class Screen2(Screen):
pass
sm = ScreenManager()
sm.add_widget(Screen1(name= 'screen1'))
sm.add_widget(Screen2(name= 'screen2'))
class MainApp(MDApp):
def build(self):
sm = Builder.load_file("Main.kv")
return sm
MainApp().run()
Upvotes: 0