0

我正在尝试为我的应用程序创建一个登录页面。为此,我正在使用 kivymd。我希望通过我的终端打印用户输入,并且在我使用屏幕管理器分隔我的屏幕之前它也是如此。

为此,我在 .kv 中使用了带有按钮的 on_release 并调用了该函数。但它给出了以下错误:


  File "kivy\properties.pyx", line 860, in kivy.properties.ObservableDict.__getattr__
 KeyError: 'usernamecreate'
 
 During handling of the above exception, another exception occurred:
 
   File "C:/Users/SAMSUNG/PycharmProjects/SchoolApp/main.py", line 51, in create
     print(self.root.ids.usernamecreate.text)
   File "kivy\properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'


我只提供了错误的开头和结尾行,因为整行真的很长。

这是我的 .kv 文件

ScreenManager:
    LoginScreen:
    CreateAccountScreen:

<LoginScreen>:
    name:'login'
    MDTextField:
        id:username
        hint_text:'Email or Phone number'
        helper_text:'Username cannot be left empty'
        required:True
        helper_text_mode:'on_error'
        icon_right_color:app.theme_cls.primary_color
        pos_hint:{'center_x': 0.5, 'center_y': 0.6}
        size_hint_x:None
        width:300
    MDTextField:
        id:password
        hint_text:'Enter Password'
        helper_text:'Password cannot be left empty'
        required:True
        helper_text_mode:'on_error'
        pos_hint:{'center_x': 0.5, 'center_y': 0.5}
        size_hint_x:None
        width:300
        password:True
    MDRectangleFlatButton:
        text:'Login'
        pos_hint:{'center_x': 0.5, 'center_y': 0.4}
        size_hint_x:None
    MDLabel:
        text:'-------------OR---------------'
        pos_hint:{'center_y': 0.3}
        halign:'center'
    MDFlatButton:
        text:'Create New Account'
        pos_hint:{'center_x': 0.5, 'center_y': 0.2}
        size_hint_x:None
        on_release:
            root.manager.current = 'caccount'

<CreateAccountScreen>:
    name:'caccount'
    MDTextField:
        id:usernamecreate
        hint_text:'Email or Phone number'
        helper_text:'Username cannot be left empty'
        required:True
        helper_text_mode:'on_error'
        icon_right_color:app.theme_cls.primary_color
        pos_hint:{'center_x': 0.5, 'center_y': 0.7}
        size_hint_x:None
        width:300
    MDTextField:
        id:password
        hint_text:'Enter Password'
        helper_text:'Password cannot be left empty'
        required:True
        helper_text_mode:'on_error'
        pos_hint:{'center_x': 0.5, 'center_y': 0.6}
        size_hint_x:None
        width:300
        password:True
    MDTextField:
        id:password1
        hint_text:'Confirm Password'
        helper_text:'Password cannot be left empty'
        required:True
        helper_text_mode:'on_error'
        pos_hint:{'center_x': 0.5, 'center_y': 0.5}
        size_hint_x:None
        width:300
        password:True
    MDRectangleFlatButton:
        text:'Create Account'

        pos_hint:{'center_x': 0.5, 'center_y': 0.4}
        size_hint_x:None
        on_release:app.create()
    MDLabel:
        text:'-------------OR---------------'
        pos_hint:{'center_y': 0.3}
        halign:'center'
    MDFlatButton:
        text:'Login'
        pos_hint:{'center_x': 0.5, 'center_y': 0.2}
        size_hint_x:None
        on_release:
            root.manager.current = 'login'
        **#Here I have called the function**


我不认为调用函数会出错,但看看函数内部发生了什么:

class SchoolApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette = "Teal"
        return
    def create(self):
        print(self.root.ids.usernamecreate.text)
        print(self.root.ids.password.text)


请建议我可以做些什么来消除错误,因为其他一切工作正常我没有放整个代码。但是,如果你想看的话,请告诉我这样做。

提前致谢

4

1 回答 1

0

您可以更改如下create功能来解决问题:

    def create(self):
        print(self.root.children[0].ids.usernamecreate.text)
        print(self.root.children[0].ids.password.text)
于 2020-08-29T15:01:22.687 回答