KivyMD屏幕管理器,无法正常运行

时间:2019-12-30 16:27:07

标签: python-3.x kivy kivy-language

我指的是this video,并尝试使用KivyMD复制相同的内容。基本上,这是带有屏幕管理器的简单应用。输入密码 pswd 后,您将进入下一个屏幕,并在释放按钮时返回。

我正在尝试用KivyMD TestRoundField替换文本

教程中的

main.py 文件

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 文件-密码- 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"

我的代码


这是我的 main.py 文件-

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

这是我的 loginKivy.kv 文件

#: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

我对如何继续进行感到困惑。接下来我该怎么办?

1 个答案:

答案 0 :(得分:0)

我建议将您的build()方法更改为:

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

然后,您需要向Buttonskv规则中添加“提交”和“返回” <Second_Screen>

您可以使用kivy Buttons或kivyMD Button添加这些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"

我还添加了ScreenManager结构,允许build()方法只返回加载kv文件的结果。

kv的文档位于kivy.lang