>source

여기에 문제가 발생하는 .py 측면에서 내 code의 exerpt입니다.

from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
Kv_loader= """
ScreenManager:
    LoginScreen:
    ProfileScreen:
<LoginScreen>:
    name: 'login'
    Screen:
        MDNavigationLayout:
            ScreenManager:
                Screen:
                    BoxLayout:
                        orientation:'vertical'
                        MDToolbar:
                            title: "My Demo App"
                            elevation: 10
                            right_action_items : [["rotate-3d-variant"]]
                        MDBottomNavigation:
                            MDBottomNavigationItem:
                                name: 'screen 1'
                                text: 'Login'
                                icon: 'login'
                                BoxLayout:
                                    orientation: "vertical"
                                    size_hint_y: None
                                    height: self.minimum_height
                                    pos_hint: {'top':0.9}
                                    MDLabel:
                                        font_style: "H4"
                                        theme_text_color: "Primary"
                                        text: "Login Form"
                                        halign: "center"
                                BoxLayout:
                                    orientation: "vertical"
                                    size_hint_y: None
                                    height: self.minimum_height
                                    spacing: dp(15)
                                    pos_hint: {'top':0.74}
                                    MDTextField:
                                        id: user_id
                                        hint_text:"Enter user name"
                                        icon_right:"account"
                                        pos_hint:{'center_x':0.5, 'center_y':0.5}
                                        size_hint_x:0.76
                                    MDTextField:
                                        id: password_t
                                        hint_text:"Enter password"
                                        icon_right:"eye-off"
                                        password: True
                                        pos_hint:{'center_x':0.5, 'center_y':0.5}
                                        size_hint_x:0.76
                                BoxLayout:
                                    orientation: "vertical"
                                    size_hint_y: None
                                    height: self.minimum_height
                                    spacing: dp(30)
                                    pos_hint: {'top':0.38}
                                    MDFillRoundFlatButton:
                                        text:'Login'
                                        size_hint_x:0.4
                                        font_size:17
                                        pos_hint:{'center_x':0.5, 'center_y':0.3}
                                        on_press: app.login_fun()
                            MDBottomNavigationItem:
                                name: 'screen 2'
                                text: 'Sign Up'
                                icon: 'account-plus'
                                BoxLayout:
                                    orientation: "vertical"
                                    size_hint_y: None
                                    height: self.minimum_height
                                    spacing: dp(30)
                                    pos_hint: {'top':0.9}
                                    MDLabel:
                                        text: "Sign up form"
                                        font_style: "H5"
                                        theme_text_color: "Primary"
                                        halign: "center"
<ProfileScreen>:
    name: 'profile'
    id: 'profile'
    Screen:
        BoxLayout:
            orientation:'vertical'
            MDToolbar:
                title:'Demo Applicationeffe'
                left_action_items : [["arrow-left"]]
                right_action_items : [["dots-vertical"]]
                elevation:10
            MDLabel:
                text:'Profile user'
                halign:'center'
"""
class LoginScreen(Screen):
    pass
class ProfileScreen(Screen):
    pass
sm= ScreenManager()
sm.add_widget(LoginScreen(name='login'))
sm.add_widget(ProfileScreen(name='profile'))
class MyApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette="Blue"
        self.theme_cls.theme_style="Light"
        self.theme_cls.primary_hue="A700"
        self.screen= Builder.load_string(Kv_loader)
        return self.screen
    def login_fun(self):
        print(self.screen.ids.user_id.text)
if __name__=='__main__':
    MyApp().run()

메인 클래스에서 사용자 입력 값을 얻으려고합니다. 그러나 오류가 표시됩니다.

>  Traceback (most recent call last):
> File "kivy\properties.pyx", line 861, in
> kivy.properties.ObservableDict.__getattr__
> KeyError: 'user_id'
>>                                                                                                                                                                 During handling of the above exception, another exception occurred:
>>                                                                                                                                                                 Traceback (most recent call last):
> File "D:\Working_dir\Programming\Python\Kivy-MD\test.py", line 164, in
> <module>>>      MyApp().run()                                                                                                                                                File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\app.py",
> line 950, in run
>      runTouchApp()                                                                                                                                                File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\base.py",
> line 582, in runTouchApp
>      EventLoop.mainloop()                                                                                                                                         File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\base.py",
> line 347, in mainloop
>      self.idle()                                                                                                                                                  File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\base.py",
> line 391, in idle
>      self.dispatch_input()                                                                                                                                        File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\base.py",
> line 342, in dispatch_input
>      post_dispatch_input(*pop(0))                                                                                                                                 File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\base.py",
> line 248, in post_dispatch_input
>      listener.dispatch('on_motion', etype, me)                                                                                                                    File "kivy\_event.pyx", line 709, in
> kivy._event.EventDispatcher.dispatch
> File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\core\window\__init__.py",
> line 1412, in on_motion
>      self.dispatch('on_touch_down', me)                                                                                                                           File "kivy\_event.pyx", line 709, in
> kivy._event.EventDispatcher.dispatch
> File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\core\window\__init__.py",
> line 1428, in on_touch_down
>      if w.dispatch('on_touch_down', touch):                                                                                                                       File "kivy\_event.pyx", line 709, in
> kivy._event.EventDispatcher.dispatch
> File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\screenmanager.py",
> line 1198, in on_touch_down
>      return super(ScreenManager, self).on_touch_down(touch)                                                                                                       File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py",
> line 545, in on_touch_down
>      if child.dispatch('on_touch_down', touch):                                                                                                                   File "kivy\_event.pyx", line 709, in
> kivy._event.EventDispatcher.dispatch
> File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\relativelayout.py",
> line 297, in on_touch_down
>      ret= super(RelativeLayout, self).on_touch_down(touch)                                                                                                       File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py",
> line 545, in on_touch_down
>      if child.dispatch('on_touch_down', touch):                                                                                                                   File "kivy\_event.pyx", line 709, in
> kivy._event.EventDispatcher.dispatch
> File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\relativelayout.py",
> line 297, in on_touch_down
>      ret= super(RelativeLayout, self).on_touch_down(touch)                                                                                                       File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py",
> line 545, in on_touch_down
>      if child.dispatch('on_touch_down', touch):                                                                                                                   File "kivy\_event.pyx", line 709, in
> kivy._event.EventDispatcher.dispatch
> File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py",
> line 545, in on_touch_down
>      if child.dispatch('on_touch_down', touch):                                                                                                                   File "kivy\_event.pyx", line 709, in
> kivy._event.EventDispatcher.dispatch
> File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\screenmanager.py",
> line 1198, in on_touch_down
>      return super(ScreenManager, self).on_touch_down(touch)                                                                                                       File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py",
> line 545, in on_touch_down
>      if child.dispatch('on_touch_down', touch):                                                                                                                   File "kivy\_event.pyx", line 709, in
> kivy._event.EventDispatcher.dispatch
> File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\relativelayout.py",
> line 297, in on_touch_down
>      ret= super(RelativeLayout, self).on_touch_down(touch)                                                                                                       File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py",
> line 545, in on_touch_down
>      if child.dispatch('on_touch_down', touch):                                                                                                                   File "kivy\_event.pyx", line 709, in
> kivy._event.EventDispatcher.dispatch
> File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py",
> line 545, in on_touch_down
>      if child.dispatch('on_touch_down', touch):                                                                                                                   File "kivy\_event.pyx", line 709, in
> kivy._event.EventDispatcher.dispatch
> File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py",
> line 545, in on_touch_down
>      if child.dispatch('on_touch_down', touch):                                                                                                                   File "kivy\_event.pyx", line 709, in
> kivy._event.EventDispatcher.dispatch
> File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\screenmanager.py",
> line 1198, in on_touch_down
>      return super(ScreenManager, self).on_touch_down(touch)                                                                                                       File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py",
> line 545, in on_touch_down
>      if child.dispatch('on_touch_down', touch):                                                                                                                   File "kivy\_event.pyx", line 709, in
> kivy._event.EventDispatcher.dispatch
> File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\relativelayout.py",
> line 297, in on_touch_down
>      ret= super(RelativeLayout, self).on_touch_down(touch)                                                                                                       File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py",
> line 545, in on_touch_down
>      if child.dispatch('on_touch_down', touch):                                                                                                                   File "kivy\_event.pyx", line 709, in
> kivy._event.EventDispatcher.dispatch
> File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py",
> line 545, in on_touch_down
>      if child.dispatch('on_touch_down', touch):                                                                                                                   File "kivy\_event.pyx", line 709, in
> kivy._event.EventDispatcher.dispatch
> File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivymd\uix\behaviors\ripple_behavior.py",
> line 255, in on_touch_down
>      super().on_touch_down(touch)                                                                                                                                 File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivymd\uix\button.py",
> line 969, in on_touch_down
>      return super().on_touch_down(touch)                                                                                                                          File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\behaviors\button.py",
> line 151, in on_touch_down
>      self.dispatch('on_press')                                                                                                                                    File "kivy\_event.pyx", line 705, in
> kivy._event.EventDispatcher.dispatch
> File "kivy\_event.pyx", line 1248, in
> kivy._event.EventObservers.dispatch
> File "kivy\_event.pyx", line 1132, in
> kivy._event.EventObservers._dispatch
> File
> "C:\Users\Shourov\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\lang\builder.py",
> line 57, in custom_callback
>      exec(__kvlang__.co_value, idmap)                                                                                                                             File "<string>", line 70, in <module>> File "D:\Working_dir\Programming\Python\Kivy-MD\test.py", line 159, in
> login_fun
>>      print(self.screen.ids.user_id.text)                                                                                                                          File "kivy\properties.pyx", line 864, in
> kivy.properties.ObservableDict.__getattr__
> AttributeError: 'super' object has no attribute '__getattr__'
> [Finished in 4.30s with exit code 1]

보았 듯이 로그인 버튼을 누르면 TextField의 텍스트를 가져오고 싶지만 그 일을하는 더 좋은 방법이있을 수 있습니다. 누구 든지이 오류의 이유 와이 문제를 극복하는 방법을 설명 할 수 있습니까?

최소한의 재현 가능한 예제를 만들 수 있습니까? 레이아웃의 어떤 부분이 문제를 일으키는 것과 관련이있는 것처럼 보입니다. 또한 별도의 .kv 파일을 사용하지 않는 이유는 무엇입니까?

Karl Knechtel2021-09-15 04:24:11

못생긴 code에 대해 죄송합니다. 실제로 나는 초보자 이므로이 스타일에서 편안함을 느낄 수 있습니다. LoginScreen 클래스>MDBottomNavigation>MDBottomNavItigationEM (화면 1)>두 번째 BoxLayout>TextField (ID : use_id). MyApp 클래스에서 TextField 텍스트를 가져 오려고합니다.

Shourov2021-09-15 04:24:11

내 레이아웃을 사용하여 문제를 재현 할 수 있습니까?

Karl Knechtel2021-09-15 04:24:11

나는 더 간단한 레이아웃을 사용하여 문제를 재현했습니다. 이 code를 확인하십시오.

Shourov2021-09-15 04:24:11
  • 이전 angularjs : AG-Grid isrowselectable 조건부가 활성화되지 않습니다
  • 다음 python : "django.db.utils.ProgrammingError : 관계"silk_request "는 무엇이 존재하지 않습니다. 1 :"silk_reque의 의미가 있습니까?