首页 > 解决方案 > KivyMD 按下按钮时切换屏幕?

问题描述

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

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 很陌生,所以请多多包涵:-)

标签: pythonkivykivy-language

解决方案


您的代码存在问题:

  • 您必须为您的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"
    

推荐阅读