How can I switch between screens that utilize both KVLang and Python in Kivy/KivyMD with the press of a button?
I found this post from about five years ago in which the user can switch between KV files, but I need to be able to transition from one screen that has distinct elements defined in both a Python and a KV file to another screen that has different distinct elements defined in the same way.
For example, I need to be able to switch between screens similar to these two:
screen_1.py
from kivymd.app import MDApp
from kivy.lang import Builder
class Screen1(MDApp):
def misc_func(self, *args):
[...]
def go_to_screen_2(self):
"""Goes to screen 2"""
def build(self):
return Builder.load_file("screen_1.kv")
if __name__ == "__main__":
Screen1().run()
screen_1.kv
MDScreen:
FloatLayout:
MDTopAppBar:
title: "Screen 1"
pos_hint: {'top': 1}
MDRaisedButton:
text: "misc func"
on_press: app.misc_func()
pos_hint: {'center_x': 0.5, 'center_y': 0.6}
MDRaisedButton:
text: "go to screen 2"
pos_hint: {'center_x': 0.5, 'center_y': 0.4}
on_press: app.go_to_screen_2()
screen_2.py
from kivymd.app import MDApp
from kivy.lang import Builder
class Screen2(MDApp):
def go_to_screen_1(self):
"""Goes to screen 1"""
def a_different_misc_func(self, *args):
[...]
def build(self):
return Builder.load_file("screen_2.kv")
if __name__ == "__main__":
Screen2().run()
screen_2.kv
MDScreen:
FloatLayout:
MDTopAppBar:
title: "Screen 2"
pos_hint: {'top': 1}
MDRaisedButton:
text: "misc func"
on_press: app.a_different_misc_func()
pos_hint: {'center_x': 0.5, 'center_y': 0.6}
MDRaisedButton:
text: "go to screen 1"
pos_hint: {'center_x': 0.5, 'center_y': 0.4}
on_press: app.go_to_screen_1()