0

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()
Zach M.
  • 31
  • 4

1 Answers1

0

The ScreenManager widget in Kivy/KivyMD allows you to quickly transition between screens that use both KVLang and Python. The ScreenManager widget enables animated switching between many screens while managing them. Here is an illustration of how to change the code you supplied to make advantage of the ScreenManager widget: main.py

python
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp

class Screen1(Screen):
def misc_func(self, *args):
    pass

def go_to_screen_2(self):
    self.manager.current = 'screen_2'

class Screen2(Screen):
def go_to_screen_1(self):
    self.manager.current = 'screen_1'

def a_different_misc_func(self, *args):
    pass

class WindowManager(ScreenManager):
pass

class MainApp(MDApp):
def build(self):
    Builder.load_file('main.kv')
    return WindowManager()

`

if name == 'main': MainApp().run()

main.kv

kv
<WindowManager>:
Screen1:
    name: 'screen_1'
Screen2:
    name: 'screen_2'

<Screen1>:
BoxLayout:
    orientation: 'vertical'

    MDToolbar:
        title: 'Screen 1'

    MDRaisedButton:
        text: 'misc func'
        on_press: root.misc_func()

    MDRaisedButton:
        text: 'go to screen 2'
        on_press: root.go_to_screen_2()

<Screen2>:
BoxLayout:
    orientation: 'vertical'

    MDToolbar:
        title: 'Screen 2'

    MDRaisedButton:
        text: 'a different misc func'
        on_press: root.a_different_misc_func()

    MDRaisedButton:
        text: 'go to screen 1'
        on_press: root.go_to_screen_1()
gene
  • 1
  • 3
  • Putting all the python code in the same file and all the KV in another file is not really an option as for what I am using this for has seven screens with quite a lot of code in each. I really need them to be confined in their own files. – Zach M. Jul 09 '23 at 00:02