KivyMD 从文本字段获取属性错误 python3 KivyMD

KivyMD Get attribute error from textfield python3 KivyMD

这是代码:

from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

KV = """
ScreenManager:
    MainScreen:
    LoginScreen:
    RegisterScreen:
<MainScreen>:
    name: 'main'
    MDLabel:
        text:"Çetele"
        font_style:"H3"
        pos_hint:{'center_x':0.5,'center_y':0.94}
    MDRectangleFlatButton:
        text: 'Giriş Yap'
        pos_hint: {'center_x':0.5,'center_y':0.6}
        on_press: root.manager.current = 'login'
    MDRectangleFlatButton:
        text: 'Kayıt Ol'
        pos_hint: {'center_x':0.5,'center_y':0.5}
        on_press: root.manager.current = 'register'
    
<LoginScreen>:
    name: 'login'
    
    MDToolbar:
        title:"Çetele Giriş Yap"
        pos_hint: {'top':1}

    MDTextField:
        id:'email'
        size_hint_x:None
        width:400
        pos_hint:{'center_x':0.5,'center_y':0.6}
        hint_text:"E-Mail"
        helper_text: "E-Mail"

    MDTextField:
        name:"password"
        size_hint_x:None
        width:400
        pos_hint:{'center_x':0.5,'center_y':0.5}
        hint_text:"Şifre"
        helper_text: "E-Mail"


    MDRectangleFlatButton:
        text:"Contiune"
        on_release: app.Login()



    MDIconButton:
        icon: 'arrow-left'
        pos_hint: {'center_x':0.09,'center_y':0.1}
        on_press: root.manager.current = 'main'
        





<RegisterScreen>:
    name: 'register'
    MDToolbar:
        title:"Çetele - Kayıt ol"
        pos_hint:{'top':1}
    MDTextField:
        name:"email"
        size_hint_x:None
        width:400
        pos_hint:{'center_x':0.5,'center_y':0.6}
        hint_text:"E-Mail"
        helper_text: "E-Mail"

    MDTextField:
        name:"password"
        size_hint_x:None
        width:400
        pos_hint:{'center_x':0.5,'center_y':0.5}
        hint_text:"Şifre"
        helper_text: "E-Mail"


    MDIconButton:
        icon: 'arrow-left'
        pos_hint: {'center_x':0.09,'center_y':0.1}
        on_press: root.manager.current = 'main'
        
"""


class MainScreen(Screen):
    pass


class LoginScreen(Screen):
    pass


class RegisterScreen(Screen):
    pass


# Create the screen manager
sm = ScreenManager()
sm.add_widget(MainScreen(name='main'))
sm.add_widget(LoginScreen(name='login'))
sm.add_widget(RegisterScreen(name='register'))


class DemoApp(MDApp):
    
    def build(self):
        self.theme_cls.primary_palette = "Blue"
        self.theme_cls.primary_hue = "A700"
        self.screen = Builder.load_string(KV)

        return self.screen
    def Login(self):
        text = self.root.ids
        print(text)

    

DemoApp().run()

错误是:

 Traceback (most recent call last):
   File "kivy/properties.pyx", line 860, in kivy.properties.ObservableDict.__getattr__
 KeyError: 'email'
 
 During handling of the above exception, another exception occurred:
 
 Traceback (most recent call last):
   File "logoAndLoad.py", line 128, in <module>
     DemoApp().run()
   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/app.py", line 855, in run
     runTouchApp()
   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/base.py", line 504, in runTouchApp
     EventLoop.window.mainloop()
   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/core/window/window_sdl2.py", line 747, in mainloop
     self._mainloop()
   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/core/window/window_sdl2.py", line 479, in _mainloop
     EventLoop.idle()
   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/base.py", line 342, in idle
     self.dispatch_input()
   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/base.py", line 327, in dispatch_input
     post_dispatch_input(*pop(0))
   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/base.py", line 293, in post_dispatch_input
     wid.dispatch('on_touch_up', me)
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivymd/uix/behaviors/ripplebehavior.py", line 245, in on_touch_up
     return super().on_touch_up(touch)
   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivymd/uix/button.py", line 969, in on_touch_up
     return super().on_touch_up(touch)
   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/uix/behaviors/button.py", line 179, in on_touch_up
     self.dispatch('on_release')
   File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
   File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
   File "kivy/_event.pyx", line 1098, in kivy._event.EventObservers._dispatch
   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/lang/builder.py", line 64, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "<string>", line 47, in <module>
   File "logoAndLoad.py", line 123, in Login
     text = self.root.ids.email
   File "kivy/properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

我想从文本字段中获取文本,但它给出了 error.Can 你能帮忙吗?

我正在做一个登录系统和数据库应用程序,但我坚持了一个星期。

我需要一个答案,我需要完成这个。

为什么会出现这个错误: 文件“kivy/properties.pyx”,第 860 行,在 kivy.properties.ObservableDict.getattr 中 键错误:'email'

首先删除您的文本输入 ID 上的引号,您不需要它们:

MDTextField:
    id:email
    size_hint_x:None
    width:400
    pos_hint:{'center_x':0.5,'center_y':0.6}
    hint_text:"E-Mail"
    helper_text: "E-Mail"

然后,在您的 Login 函数上,在应用程序 class 内,您必须先获得正确的屏幕,然后您才能使用 ids 检索正确的元素和它是文本:

def Login(self):
    login_screen = self.root.get_screen('login')
    print(login_screen.ids.email.text)