Search code examples
pythonpython-3.xkivykivy-languagekivymd

Changing the screen and running a function on kivyMD?


I am learning Kivy, but I do not kown how to change a screen and running a funtion at the same time.

Where should I declare my funtion so the button have access to the code and can run the function?

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

screen_helper = """
ScreenManager:
    MenuScreen:
    FunctionScreen:

<MenuScreen>:
    name: 'menu'
    MDRectangleFlatButton:
        text: 'Function'
        pos_hint: {'center_x':0.5,'center_y':0.5}
        on_press: root.manager.current = 'function screen'

<FunctionScreen>:
    name: 'function screen'
    MDRectangleFlatButton:
        text: 'Back'
        pos_hint: {'center_x':0.5,'center_y':0.1}
        on_press: root.manager.current = 'menu'

"""


class MenuScreen(Screen):
    pass


class FunctionScreen(Screen):
    pass


# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(FunctionScreen(name='function'))


class DemoApp(MDApp):

    def build(self):
        screen = Builder.load_string(screen_helper)
        return screen

    # def funtion(self):
        # do stuff and then go to menu screen



DemoApp().run()

Should I try maybe, add the on_opress atribute in the build function?

Can you guys help me?


Solution

  • There are several convenient places to place the function(). One is in the MenuScreen, and in that case, it would be referenced in the kv files as:

    root.function()
    

    Another convenient place is in the DemoApp, and in that case, the reference would be:

    app.function()
    

    So, here is a version of your code tht puts the function() in the App:

    from kivymd.app import MDApp
    from kivy.lang.builder import Builder
    from kivy.uix.screenmanager import Screen
    
    screen_helper = """
    ScreenManager:
        MenuScreen:
        FunctionScreen:
    
    <MenuScreen>:
        name: 'menu'
        MDRectangleFlatButton:
            text: 'Function'
            pos_hint: {'center_x':0.5,'center_y':0.5}
            on_press:
                root.manager.current = 'function screen'
                app.function()
    
    <FunctionScreen>:
        name: 'function screen'
        MDRectangleFlatButton:
            text: 'Back'
            pos_hint: {'center_x':0.5,'center_y':0.1}
            on_press: root.manager.current = 'menu'
    
    """
    
    
    class MenuScreen(Screen):
        pass
    
    
    class FunctionScreen(Screen):
        pass
    
    
    class DemoApp(MDApp):
    
        def build(self):
            sm = Builder.load_string(screen_helper)
            return sm
    
        def function(self):
            # do stuff and then go to menu screen
            print('in function')
    
    
    DemoApp().run()
    

    Note that the lines of your code that built a ScreenManager have been deleted as they are unnecessary.