简体   繁体   中英

I need help debugging kivy screen manager code

I am fairly new to using kivy and am attempting to create a multi screen application using the screen manager. I previously had the application working, but the only way I could navigate between screens was by code that existed within the.kv file not the.py file of the app. For me to continue with my project, I need to be able to update the current screen from the.py file.

Beneath is a copy of my code (both the.py file and the corresponding.kv file) as you can see on line 10 and 11 of the.py file I have attempted some code to update the current screen that I found online. All that this code seems to do is cause the screen I'm on to perform an animation as if its changing screen, but then it doesn't actually change. Either suggestions for fixing this code or re writing it in a more efficient way are very welcome.

.py file

from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition


class SCRNLogin(Screen):
    def __init__(self, **kwargs):
        super(SCRNLogin, self).__init__(**kwargs)

    def login(self, password):
        if password == "bananas":
            self.parent.current = "second"


class SecondWindow(Screen):
    pass


class WindowManager(ScreenManager):
    pass


class AsteriaApp(MDApp):
    def build(self):
        self.icon = "Media/Images/Asteria_Icon_Blue.png"
        self.theme_cls.primary_palette = "Purple"
        self.theme_cls.accent_palette = "Pink"
        self.theme_cls.theme_style = "Light"
        sm = WindowManager()
        sm.add_widget(SCRNLogin())
        sm.add_widget(SecondWindow())
        return sm


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

.kv file

<SCRNLogin>:
    name: "login"
    BoxLayout:
        orientation: "vertical"

        MDLabel:
            text: "Password: "

        TextInput:
            id: passw
            multiline: False

        MDRaisedButton:
            text: "Submit"
            on_release:
                root.login(passw.text)
                app.root.current = "second" if passw.text == "tim" else "login"
                root.manager.transition.direction = "left"

<SecondWindow>:
    name: "second"
    BoxLayout:
        orientation: "vertical"

        MDRaisedButton:
            text: "Go Back"
            on_release:
                app.root.current = "login"
                root.manager.transition.direction = "right"

        MDSwitch:
            widget_style: "ios"

That is happening because your kv code says:

    MDRaisedButton:
        text: "Submit"
        on_release:
            root.login(passw.text)
            app.root.current = "second" if passw.text == "tim" else "login"
            root.manager.transition.direction = "left"

which calls the root.login() method:

def login(self, password):
    if password == "bananas":
        self.parent.current = "second"

That method changes the current Screen to the SecondWindow (if the passw is bananas ). Then

app.root.current = "second" if passw.text == "tim" else "login"

changes the current Screen back to the SCRNLogin . You should probably adjust your current Screen in one place or the other, and not have opposing changes being made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM