<bdo id='8fDId'></bdo><ul id='8fDId'></ul>

      <tfoot id='8fDId'></tfoot>

    1. <legend id='8fDId'><style id='8fDId'><dir id='8fDId'><q id='8fDId'></q></dir></style></legend>
    2. <i id='8fDId'><tr id='8fDId'><dt id='8fDId'><q id='8fDId'><span id='8fDId'><b id='8fDId'><form id='8fDId'><ins id='8fDId'></ins><ul id='8fDId'></ul><sub id='8fDId'></sub></form><legend id='8fDId'></legend><bdo id='8fDId'><pre id='8fDId'><center id='8fDId'></center></pre></bdo></b><th id='8fDId'></th></span></q></dt></tr></i><div id='8fDId'><tfoot id='8fDId'></tfoot><dl id='8fDId'><fieldset id='8fDId'></fieldset></dl></div>

    3. <small id='8fDId'></small><noframes id='8fDId'>

        Kivy:使用“on focus"或“on_touch_down"清除文本

        时间:2023-10-09
          <tbody id='4iMc2'></tbody>
      1. <small id='4iMc2'></small><noframes id='4iMc2'>

      2. <tfoot id='4iMc2'></tfoot>

          <i id='4iMc2'><tr id='4iMc2'><dt id='4iMc2'><q id='4iMc2'><span id='4iMc2'><b id='4iMc2'><form id='4iMc2'><ins id='4iMc2'></ins><ul id='4iMc2'></ul><sub id='4iMc2'></sub></form><legend id='4iMc2'></legend><bdo id='4iMc2'><pre id='4iMc2'><center id='4iMc2'></center></pre></bdo></b><th id='4iMc2'></th></span></q></dt></tr></i><div id='4iMc2'><tfoot id='4iMc2'></tfoot><dl id='4iMc2'><fieldset id='4iMc2'></fieldset></dl></div>

          <legend id='4iMc2'><style id='4iMc2'><dir id='4iMc2'><q id='4iMc2'></q></dir></style></legend>

                  <bdo id='4iMc2'></bdo><ul id='4iMc2'></ul>
                  本文介绍了Kivy:使用“on focus"或“on_touch_down"清除文本输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想在单击 TextInputtext: 时清除它.示例代码:

                  I want to clear a TextInput's text: when I click on it. Sample Code:

                  from kivy.app import App
                  from kivy.lang import Builder
                  
                  kv_string = """
                  ScreenManager:
                      id: manager
                      Screen:
                          BoxLayout:
                              orientation: 'vertical'
                              Button:
                                  text: 'Why does it clear multiple inputs? And why do they get cleared after touch_up?'
                              TextInput:
                                  text: 'Write Your Name'
                                  on_touch_down:
                                      self.text = ''
                  
                              TextInput:
                                  text: 'Write Your Last Name'
                                  on_focus:
                                      self.text = ''
                  
                              TextInput:
                                  text: 'Write Your Phone Number'
                                  on_touch_down:
                                      self.text = ''
                  """
                  
                  class MyApp(App):
                  
                      def build(self):
                          root_widget = Builder.load_string(kv_string)
                          return root_widget
                  
                  if __name__ == "__main__":
                      MyApp().run()
                  

                  on_touch_down:on_focus 都不会删除当前聚焦的文本输入.相反,当我触摸屏幕上的任何位置时,两者都会被清除.一旦光标位于文本输入上,我希望它们单独清除.我也试过 on_cursor 但这也没有用.我错过了什么?提前致谢!

                  Neither on_touch_down: or on_focus erases JUST the text input that is currently focused. Instead, both get cleared when I touch anywhere on the screen. I would want them cleared individually once the cursor is on a text input. I also tried on_cursor but that didn't work either. What am I missing? Thank you in advance!

                  推荐答案

                  on_touch_down 事件被所有小部件接收,直到一个返回 True 告诉事件循环它正在使用它,因此没有将其发送到 文档:

                  The on_touch_down event is received by all the widgets until one returns True telling the event-loop that it is using it and thus not send it to other widgets as indicated by the docs:

                  on_touch_down(touch) 在 1.0.0 中添加

                  on_touch_down(touch) Added in 1.0.0

                  接收触地事件.

                  参数:

                  touch: MotionEvent 类 Touch 收到.

                  touch: MotionEvent class Touch received.

                  触摸在父坐标中.有关坐标的讨论,请参见 relativelayout系统.

                  The touch is in parent coordinates. See relativelayout for a discussion on coordinate systems.

                  返回:

                  bool 如果为 True,则触摸事件的调度将停止.如果为 False,则该事件将继续分派给其余的小部件树.

                  bool If True, the dispatching of the touch event will stop. If False, the event will continue to be dispatched to the rest of the widget tree.

                  on_touch_down 的经典用法是在 python 中,因为 kv 语言在方法覆盖方面受到限制:

                  The classic use of on_touch_down is in python since kv language is limited in the overwriting of methods:

                  from kivy.app import App
                  from kivy.lang import Builder
                  from kivy.uix.textinput import TextInput
                  
                  class MyTextInput(TextInput):
                      def on_touch_down(self, touch):
                          if self.collide_point(*touch.pos):
                              self.text = ""
                              return True
                          return super(MyTextInput, self).on_touch_down(touch)
                  
                  kv_string = """
                  ScreenManager:
                      id: manager
                      Screen:
                          BoxLayout:
                              orientation: 'vertical'
                              Button:
                                  text: 'Why does it clear multiple inputs? And why do they get cleared after touch_up?'
                              MyTextInput:
                                  text: 'Write Your Name'
                              MyTextInput:
                                  text: 'Write Your Last Name'  
                              MyTextInput:
                                  text: 'Write Your Phone Number'
                  """
                  
                  class MyApp(App):
                      def build(self):
                          root_widget = Builder.load_string(kv_string)
                          return root_widget
                  
                  if __name__ == "__main__":
                      MyApp().run()
                  

                  或 .kv 中的等价物,但最重要的是你不能返回 True.

                  Or something equivalent in .kv but the devestaja is that you can not return True.

                  kv_string = """
                  ScreenManager:
                      id: manager
                      Screen:
                          BoxLayout:
                              orientation: 'vertical'
                              Button:
                                  text: 'Why does it clear multiple inputs? And why do they get cleared after touch_up?'
                              TextInput:
                                  text: 'Write Your Name'
                                  on_touch_down: if self.collide_point(*args[1].pos): self.text = ""
                              TextInput:
                                  text: 'Write Your Last Name'
                                  on_touch_down: if self.collide_point(*args[1].pos): self.text = ""
                              TextInput:
                                  text: 'Write Your Phone Number'
                                  on_touch_down: if self.collide_point(*args[1].pos): self.text = ""
                  """
                  

                  所以你应该使用 on_focus,它是一个与 FocusBehavior 关联的事件,它会覆盖 on_touch_down 使用 self.collide_point(*touch.pos) 进行验证.

                  So you should use on_focus which is an event associated to FocusBehavior that overwrites on_touch_down verifying using self.collide_point(*touch.pos).

                  from kivy.app import App
                  from kivy.lang import Builder
                  
                  kv_string = """
                  ScreenManager:
                      id: manager
                      Screen:
                          BoxLayout:
                              orientation: 'vertical'
                              Button:
                                  text: 'Why does it clear multiple inputs? And why do they get cleared after touch_up?'
                              TextInput:
                                  text: 'Write Your Name'
                                  on_focus: self.text = ""
                              TextInput:
                                  text: 'Write Your Last Name'
                                  on_focus: self.text = ""
                              TextInput:
                                  text: 'Write Your Phone Number'
                                  on_focus: self.text = ""
                  """
                  
                  class MyApp(App):
                      def build(self):
                          root_widget = Builder.load_string(kv_string)
                          return root_widget
                  
                  if __name__ == "__main__":
                      MyApp().run()
                  

                  这篇关于Kivy:使用“on focus"或“on_touch_down"清除文本输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 Kivy for Python 中按下按钮时更新标签的文本 下一篇:如何通过按钮关闭 Kivy 弹出窗口?

                  相关文章

                  最新文章

                • <tfoot id='imxmh'></tfoot>

                    <small id='imxmh'></small><noframes id='imxmh'>

                    <legend id='imxmh'><style id='imxmh'><dir id='imxmh'><q id='imxmh'></q></dir></style></legend>
                  1. <i id='imxmh'><tr id='imxmh'><dt id='imxmh'><q id='imxmh'><span id='imxmh'><b id='imxmh'><form id='imxmh'><ins id='imxmh'></ins><ul id='imxmh'></ul><sub id='imxmh'></sub></form><legend id='imxmh'></legend><bdo id='imxmh'><pre id='imxmh'><center id='imxmh'></center></pre></bdo></b><th id='imxmh'></th></span></q></dt></tr></i><div id='imxmh'><tfoot id='imxmh'></tfoot><dl id='imxmh'><fieldset id='imxmh'></fieldset></dl></div>

                        <bdo id='imxmh'></bdo><ul id='imxmh'></ul>