首页 > 解决方案 > 如何从 MDbutton 中的 on_release 触发 MDTextField 中的 on_text_validate?

问题描述

我想on_text_validate:在我的 MDTextField 中触发:通过一个按钮。我试图通过on_release:在我的MDRaisedButton:. 我试过了,但没有用

ScreenManager:
    HomeScreen:
    ResultScreen:

<HomeScreen>:
    name: 'home'
        
    BoxLayout:
        orientation: 'vertical'
        spacing: '15dp'
        
        MDToolbar:
            title: 'Age Calculator'
            left_action_items: [["menu", lambda x: app.navigation_draw()]]
            right_action_items: [["dots-vertical", lambda x: app.info()]]            
            elevation: 20
        
        BoxLayout:
            orientation: 'vertical'

            MDLabel:

            MDTextField:
                id: userinput
                hint_text: 'DOB in "DD/MM/YYYY" Format'
                helper_text: 'Invalid Entry'
                helper_text_mode: 'on_error'
                size_hint_x: None
                width: '225dp'
                pos_hint: {"center_x": 0.5}
                halign: 'center'                
                on_quad_touch: None
                on_text: None
                on_text_validate:
                    root.inputtextfn()
                    root.text_validate()
                    root.manager.current = 'result' if self.error is False else 'home'
                
        BoxLayout:
            orientation: 'vertical'

            MDRaisedButton:
                id: btn
                text: 'SUBMIT'
                font_size: '15dp'
                elevation: 12
                md_bg_color: app.theme_cls.primary_color
                pos_hint: {"center_x": 0.5}
                
                on_release:
                    #root.ids.userinput.bind(on_text_validate=self.on_release)
                    #root.ids.userinput.on_text_validate() == True

            MDLabel:

        MDLabel:

<ResultScreen>:
    name: 'result'
    
    BoxLayout:
        orientation: 'vertical'
        spacing: '75dp'
        
        MDToolbar:
            title: 'Age Calculator'
            left_action_items: [["keyboard-backspace", lambda x: app.set_homescreen() ]]
            right_action_items: [["dots-vertical", lambda x: app.info()]]            
            elevation: 20
            type: 'top'
            pos_hint: {"top":1}

        MDLabel:

请帮助代码中的注释行。

标签: pythonkivymd

解决方案


回答我自己的问题……哈哈。这个问题简单写就解决了

root.ids.userinput.dispatch('on_text_validate')

它起作用了on_release:,现在代码看起来像这样......

ScreenManager:
  HomeScreen:
  ResultScreen:

<HomeScreen>:
  name: 'home'
      
  BoxLayout:
      orientation: 'vertical'
      spacing: '15dp'
      
      MDToolbar:
          title: 'Age Calculator'
          left_action_items: [["menu", lambda x: app.navigation_draw()]]
          right_action_items: [["dots-vertical", lambda x: app.info()]]            
          elevation: 20
      
      BoxLayout:
          orientation: 'vertical'

          MDLabel:

          MDTextField:
              id: userinput
              hint_text: 'DOB in "DD/MM/YYYY" Format'
              helper_text: 'Invalid Entry'
              helper_text_mode: 'on_error'
              size_hint_x: None
              width: '225dp'
              pos_hint: {"center_x": 0.5}
              halign: 'center'                
              on_quad_touch: None
              on_text: None
              on_text_validate:
                  root.inputtextfn()
                  root.text_validate()
                  root.manager.current = 'result' if self.error is False else 'home'
              
      BoxLayout:
          orientation: 'vertical'

          MDRaisedButton:
              id: btn
              text: 'SUBMIT'
              font_size: '15dp'
              elevation: 12
              md_bg_color: app.theme_cls.primary_color
              pos_hint: {"center_x": 0.5}
              
              on_release:
                root.ids.userinput.dispatch('on_text_validate')

          MDLabel:

      MDLabel:

<ResultScreen>:
  name: 'result'
  
  BoxLayout:
      orientation: 'vertical'
      spacing: '75dp'
      
      MDToolbar:
          title: 'Age Calculator'
          left_action_items: [["keyboard-backspace", lambda x: app.set_homescreen() ]]
          right_action_items: [["dots-vertical", lambda x: app.info()]]            
          elevation: 20
          type: 'top'
          pos_hint: {"top":1}

      MDLabel:

推荐阅读