Kivy:self.parent.current不使用屏幕管理器将我重定向到其他屏幕

时间:2020-07-23 22:41:16

标签: python kivy kivy-language kivymd

使用Kivymd,我试图使MDList中的按钮转到页面,但是当我尝试设置on_release时,什么都没有发生。有人请帮忙。我需要能够单击“退出”按钮,并使用python函数将其重定向到登录屏幕,因为在单击按钮后我还想做其他事情

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()

出勤率.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" 

1 个答案:

答案 0 :(得分:1)

您可以像这样kv稍微更改Button

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

            IconLeftWidget:
                icon: "exit-to-app" 

然后在您的AttendanceApp中添加一个方法:

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

这假设您的root中的AttendanceApp将是ScreenManager