KivyMD 按下按钮时切换屏幕?

时间:2021-06-24 14:00:32

标签: python kivy kivy-language

我正在做一个简单的登录应用程序,我似乎得到了一个

if current_property[:3] == 'on_':
 TypeError: 'NoneType' object is not subscriptable

这是我的代码:

ScreenManager:
    Password:
    Second_Screen:     

<Password@Screen>
    id: 'main'
    MDCard:
        size_hint: None, None
        size: 600, 800
        pos_hint: {'center_x': .5, "center_y": .5}
        elevation: 10
        padding: 25
        spacing: 25
        orientation: 'vertical'

        FitImage:
            source: "pfa2.jpg"

        MDLabel:
            id: welcome_label
            text: "Velkommen til PFRemmøde"
            font_size: 40
            halign: 'center'
            size_hint_y: None
            height: self.texture_size[1]
            padding_y: 15

        MDTextFieldRound:
            id: user
            hint_text: "Brugernavn"
            icon_right: "account"
            size_hint_x: None
            width: 225
            font_size: 20
            pos_hint: {"center_x": 0.5}

        MDTextFieldRound:
            id: password
            hint_text: "Password"
            icon_right: "eye-off"
            size_hint_x: None
            width: 225
            font_size: 20
            pos_hint: {"center_x": 0.5}
            password: True

        MDRoundFlatButton:
            text: "Log ind"
            width: 200
            font_size: 12
            pos_hint: {"center_x": 0.5}
            on_release:
                app.root.current = "second" if password.text =="1234" else "main"
                root.manager.transition.direction = "right"

        MDRoundFlatButton:
            text: "Afslut"
            width: 200
            font_size: 12
            pos_hint: {"center_x": 0.5}
            on_press: app.clear()

<Second_Screen>:
    id: 'second' 

    MDRoundFlatButton:
            text: "Log ind"
            width: 200
            font_size: 12
            pos_hint: {"center_x": 0.5}
            on_release:
                app.root.current = "second" if passw.text == "pswd" else "main"
                root.manager.transition.direction = "left"

我运行的对象:


class MainApp(MDApp):    
    def build(self):
        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = "Red"
        return Builder.load_file('login.kv')


def logger(self):
    self.root.ids.welcome_label.text = f'Velkommen {self.root.ids.user.text}!'
  

def clear(self):
    self.root.ids.welcome_label.text = "WELCOME"        
    self.root.ids.user.text = ""        
    self.root.ids.password.text = ""        
    
MainApp().run()

我是否遗漏了一些明显的东西?我的理想结果是,如果输入“1234”作为密码,屏幕应该切换到应用程序的“登录”版本。这似乎不起作用,但是......我对 KivyMD 很陌生,所以请耐心等待 :-)

1 个答案:

答案 0 :(得分:0)

您的代码有问题:

  • 您必须为您的 Screens 提供名称:

     ScreenManager:
         Password:
             name: 'passw'
         Second_Screen:
             name: 'second'
    
  • 您的 SecondScreen 类应该扩展 Screen

     <Second_Screen@Screen>:
    
  • 您的 SecondScreen 规则缩进不正确,这导致了您看到的错误。这是更正的版本:

     <Second_Screen@Screen>:
         id: 'second' 
    
         MDRoundFlatButton:
             text: "Log ind"
             width: 200
             font_size: 12
             pos_hint: {"center_x": 0.5}
             on_release:
                 app.root.current = "second" if passw.text == "pswd" else "main"
                 root.manager.transition.direction = "left"