Search code examples
pythonkivykivymd

How to use, how to go back to the previous screen from left_action_items of MDTopAppBar in KivyMD?


I created an app with a NavigationDrawer and everything work correctely, but the problem is to return to the first screen. I want to go back to the first screen (the Screen with name: 'Screen_*principal') when I click in the arrow-left in the MDTopAppBar in Login_*screen but nothing that I did worked.

KV file:

#: import RiseInTransition kivy.uix.screenmanager.RiseInTransition
#: import SlideTransition kivy.uix.screenmanager.SlideTransition

<CustomizeScreen@MDBoxLayout>
    orientation: 'vertical'
    text: ''
    padding: 100
    MDLabel:
        text: root.text
        halign: 'center'
        pos_hint: {'center_x': .5}
    MDFillRoundFlatButton:
        text: 'Regresar'
        pos_hint: {'center_x': .5}
        on_release:
            app.root.current = 'screen_principal'
            app.root.transition = SlideTransition(direction= 'left' )

<DrawerClickableItem@MDNavigationDrawerItem>
    focus_color: "#e7e4c0"
    text_color: "#4a4939"
    icon_color: "#4a4939"
    ripple_color: "#c5bdd2"
    selected_color: "#0c6c4d"


<DrawerLabelItem@MDNavigationDrawerItem>
    text_color: "#4a4939"
    icon_color: "#4a4939"
    focus_behavior: False
    selected_color: "#4a4939"
    _no_ripple_effect: True
    
<Screen_principal>:
    MDNavigationLayout:

            MDScreen:

                MDTopAppBar:
                    title: "Navigation Drawer"
                    elevation: 4
                    pos_hint: {"top": 1}
                    md_bg_color: "#e7e4c0"
                    specific_text_color: "#4a4939"
                    left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]

        MDNavigationDrawer:
            id: nav_drawer
            radius: (0, 16, 16, 0)

            MDNavigationDrawerMenu:

                MDNavigationDrawerHeader:
                    title: "LOCATE YOU"
                    source: "icono.png"
                    spacing: "4dp"
                    padding: "12dp", 0, 0, "56dp"

                DrawerClickableItem:
                    icon: "login"
                    text_right_color: "#4a4939"
                    text: "Iniciar Sesion"
                    on_press:
                        root.manager.current = "Login_screen"

                DrawerClickableItem:
                    icon: "new-box"
                    text: "Registrarse"
                    text_right_color: "#4a4939"
                    on_press:
                        root.current = "screen_register"

                DrawerClickableItem:
                    icon: "information-outline"
                    text: "Informacion"
                    text_right_color: "#4a4939"
                    on_press:
                        root.current = "scr1"

                DrawerClickableItem:
                    icon: "microsoft-teams"
                    text: "Equipo"
                    text_right_color: "#4a4939"
                    on_press:
                        root.current = "scr1"

<Login_screen>:
    MDBoxLayout:
        orientation: 'vertical'
        MDTopAppBar:
            title: "Login"
            md_bg_color: "#e7e4c0"
            left_action_items: [["arrow-left", lambda *args : setattr(screen_manager, "current", "Screen_principal")]]
        MDBoxLayout:
            orientation: 'vertical'
            size_hint_y: 0.3
            Image:
                source: 'usuario.png'
                pos_hint: {'center_x': 0.5}
            MDLabel:
                text: 'Iniciar Sesion'
                halign: 'center'
                color: '#FF5A1E'
                font_size: '35dp'
                bold: True
                pos_hint: {'center_y': .5}
        MDBoxLayout:
            orientation: 'vertical'
            size_hint_y: 0.3
            spacing: '12dp'
            MDTextField:
                id: user
                hint_text: "Nombre de usuario"
                icon_right: "account"
                font_size: '25dp'
                icon_right_color: '#FF5A1E'
                pos_hint: {'center_x': 0.5}
            MDTextField:
                id: password
                password:True
                icon_right: "key-variant"
                font_size: '25dp'
                icon_right_color: '#FF5A1E'
                hint_text: "Contrasena"
                pos_hint: {'center_x': 0.5}

            MDLabel:
                id: signal_login
                text: ''
                halign: 'center'
                color: '#FF5A1E'
                font_size: '15dp'

        MDBoxLayout:
            orientation: 'vertical'
            size_hint_y:0.4
            spacing: '25dp'
            MDFillRoundFlatButton:
                id: bt_ingresar
                text: 'Ingresar'
                pos_hint: {'center_x':0.5}
                on_release:
                    x = app.login_data()
                    root.current = 'open_screen' if x == True else None
            MDFillRoundFlatButton:
                md_bg_color: 1,1,1,1

Python file:

from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
import requests
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import BooleanProperty, ObjectProperty

Builder.load_file('diseno.kv')

class Screen_principal(Screen):
    pass

class Login_screen(Screen):
    pass

class TestApp(MDApp):

    def build(self):
        # Create the screen manager
        sm = ScreenManager()
        sm.add_widget(Screen_principal(name='Screen_principal'))
        sm.add_widget(Login_screen(name='Login_screen'))

        return sm

if __name__ == '__main__':
    TestApp().run()

I tried a lot of things but nothing work, if someone could fix it I would be very grateful


Solution

  • In your kv, change:

    left_action_items: [["arrow-left", lambda *args : setattr(screen_manager, "current", "Screen_principal")]]
    

    to:

    left_action_items: [["arrow-left", lambda *args : setattr(root.manager, "current", "Screen_principal")]]
    

    This change accesses the ScreenManager by referencing root.manager, and the root in that context is the Login_screen.