cg-algo
cg-algo

Reputation: 79

Kivy: self.parent.current not redirecting me to different screen using screen manager

Using Kivymd, I am trying to make a button in the MDList go to a page, but when I try to set the on_release nothing happens. Someone please help. I need to be able to click the sign out button and for it to redirect me to the sign in screen perferably using a python function because I would like to also do other things after the button is clicked

main.py

import kivy
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.behaviors.button import ButtonBehavior
from kivymd.app import MDApp
from kivymd.theming import ThemeManager

class SignInScreen(Screen):
    pass

class HomeScreen(Screen):
    pass

class MenuScreen(Screen):
    pass

class ButtonGrid(ButtonBehavior, BoxLayout):
    pass

class AttendanceApp(MDApp):
    def build(self):
        #self.theme_cls.primary_palette = "LightBlue"
        #self.theme_cls.accent_palette = "Red"
        return HomeScreen()
    def change_theme(self, primary_palette, accent_palette):
        pass
        #theme_cls = ThemeManager()
    def signin_pressed(self, instance):
        pass

if __name__ == "__main__":
    AttendanceApp().run()

Attendance.kv

# Filename: Attendance.kv

#:import utils kivy.utils

#screens

<SignInScreen>
 #code

<HomeScreen>
    NavigationLayout:

        ScreenManager:

            Screen:

                BoxLayout:
                    orientation: "vertical"

                    MDToolbar:
                        title: "Home"
                        left_action_items: [["menu", lambda x: nav_menu.set_state()]]
                        elevation: 10

                    MDLabel:
                        text: "Content"
                        halign: "center"
                        
        MDNavigationDrawer:
            id: nav_menu

            MenuScreen:

ScreenManager:
    id: sm_menu

    SignInScreen:
        name: "signin"
    HomeScreen:
        name: "home"
    MenuScreen:
        name: "menu"

<MenuScreen>
        
    ScrollView:

        MDList:
            id: scroll

            OneLineAvatarIconListItem:
                id: menu_home
                text: "Home"
                elevation: 10

                IconLeftWidget:
                    icon: "home"
            
            OneLineAvatarIconListItem:
                id: menu_schedule
                text: "Schedule"
                elevation: 10

                IconLeftWidget:
                    icon: "calendar-month"
            
            OneLineAvatarIconListItem:
                id: menu_take
                text: "Take"
                elevation: 10

                IconLeftWidget:
                    icon: "account-multiple"

            OneLineAvatarIconListItem:
                id: menu_rosters
                text: "Rosters"
                elevation: 10

                IconLeftWidget:
                    icon: "clipboard-list-outline"

            OneLineAvatarIconListItem:
                id: menu_subs
                text: "Subs"
                elevation: 10

                IconLeftWidget:
                    icon: "account-multiple-outline"
            
            OneLineAvatarIconListItem:
                id: menu_help
                text: "Help"
                elevation: 10

                IconLeftWidget:
                    icon: "lightbulb-on-outline"

            OneLineAvatarIconListItem:
                id: menu_settings
                text: "Settings"
                elevation: 10

                IconLeftWidget:
                    icon: "settings-outline"        

            OneLineAvatarIconListItem:
                id: menu_signout
                text: "Sign Out"
                elevation: 10
                on_release: "signin"

                IconLeftWidget:
                    icon: "exit-to-app" 

Upvotes: 0

Views: 682

Answers (1)

John Anderson
John Anderson

Reputation: 38947

You can change your kv slightly for that Button like this:

        OneLineAvatarIconListItem:
            id: menu_signout
            text: "Sign Out"
            elevation: 10
            on_release: app.signout_pressed()

            IconLeftWidget:
                icon: "exit-to-app" 

And in your AttendanceApp, add a method:

def signout_pressed(self):
    print('signout pressed')
    self.root.current = 'signin'

This assumes that the root of your AttendanceApp will be a ScreenManager.

Upvotes: 1

Related Questions