SohailAQ
SohailAQ

Reputation: 1030

KivyMD Screen Manager, cannot get working

I am referring to this video and trying to replicate the same using KivyMD. Basically it is a simple app with the screen manager. Once you enter the password pswd it takes you to the next screen and on the release of the button, it comes back.

I am trying to replace the text filed with KivyMD TestRoundField

main.py file from the Tutorial

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen


class MainWindow(Screen):
    pass


class SecondWindow(Screen):
    pass


class WindowManager(ScreenManager):
    pass


kv = Builder.load_file("my.kv")


class MyMainApp(App):
    def build(self):
        return kv


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

my.kv file from the tutorial - Password - pswd

WindowManager:
    MainWindow:
    SecondWindow:

<MainWindow>:
    name: "main"

    GridLayout:
        cols:1

        GridLayout:
            cols:2

            Label:
                text: "Password"

            TextInput:
                id: passw
                multiline: False
        Button:
            text: "Submit"
            on_release:
                app.root.current = "second" if passw.text == "pswd" else "main"
                root.manager.transition.direction = "left"

<SecondWindow>:
    name: "second"

    Button:
        text: "Go Back"
        on_release:
            app.root.current = "main"
            root.manager.transition.direction = "right"

My Code


This is my main.py file -

from kivy.factory import Factory
from kivymd.app import MDApp
from kivy.lang import Builder


kivyFile = Builder.load_file("loginKivy.kv")


class MainApp(MDApp):
    def __init__(self, **kwargs):
        self.title = "KivyMD Examples - Round Text Field"
        self.theme_cls.primary_palette = "BlueGray"
        super().__init__(**kwargs)

    def build(self):
        self.root = Factory.Password()
        return kivyFile

class Second_Screen(Screen):
    pass


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

This is my loginKivy.kv file

#:set color_shadow [0, 0, 0, .2980392156862745]
#:set color_lilac [.07058823529411765, .07058823529411765, .14901960784313725, .8]


<MyMDTextFieldRound@MDTextFieldRound>
    size_hint_x: None
    normal_color: color_shadow
    active_color: color_shadow
    pos_hint: {"center_x": .5}


<Password@Screen>
    canvas:
        Color:
            rgba: color_lilac
        Rectangle:
            pos: self.pos
            size: self.size

    BoxLayout:
        orientation: "vertical"
        size_hint_y: None
        height: self.minimum_height
        spacing: dp(15)
        pos_hint: {"center_x": .5, "center_y": .5}

        MyMDTextFieldRound:
            icon_type: "without"
            hint_text: "Field without icon"
            color: 1,0,1,1

I am confused on how to proceed further. What should I do next?

Upvotes: 1

Views: 6322

Answers (1)

John Anderson
John Anderson

Reputation: 38837

I suggest changing your build() method to:

def build(self):
    return Builder.load_file("loginKivy.kv")
    # sm = ScreenManager()
    # sm.add_widget(Factory.Password())
    # sm.add_widget(Second_Screen())
    # return sm

Then you need to add the "Submit" and "Go Back" Buttons to your kv as well as the <Second_Screen> rule.

You can add those Buttons using kivy Button or kivyMD MDRaisedButton:

#:set color_shadow [0, 0, 0, .2980392156862745]
#:set color_lilac [.07058823529411765, .07058823529411765, .14901960784313725, .8]

ScreenManager:
    Password:
    Second_Screen:     

<MyMDTextFieldRound@MDTextFieldRound>
    size_hint_x: None
    normal_color: color_shadow
    active_color: color_shadow
    pos_hint: {"center_x": .5}


<Password@Screen>
    name: "main"
    canvas:
        Color:
            rgba: color_lilac
        Rectangle:
            pos: self.pos
            size: self.size

    BoxLayout:
        orientation: "vertical"
        size_hint_y: None
        height: self.minimum_height
        spacing: dp(15)
        pos_hint: {"center_x": .5, "center_y": .5}

        MyMDTextFieldRound:
            id: passw
            icon_type: "without"
            hint_text: "Field without icon"
            color: 1,0,1,1

        MDRaisedButton:
            text: "Submit"
            pos_hint: {'center_x': 0.5}
            on_release:
                app.root.current = "second" if passw.text == "pswd" else "main"
                root.manager.transition.direction = "left"

<Second_Screen>:
    name: 'second' 

    MDRaisedButton:
        text: "Go Back"
        pos_hint: {'center_x': 0.5, 'center_y': 0.5}
        on_release:
            app.root.current = "main"
            root.manager.transition.direction = "right"

I have also added the ScreenManager structure allowing the build() method to just return the result of loading the kv file.

The documentation for kv can be found at kivy.lang

Upvotes: 2

Related Questions