Kivy:使用“焦点对准”或“ on_touch_down”清除文本输入

时间:2018-11-01 18:15:25

标签: python kivy

我想在单击时清除TextInput的{​​{1}}。 示例代码:

text:

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,但是那也不起作用。我想念什么? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

所有小部件都接收on_touch_down事件,直到返回True告诉事件循环它正在使用它,从而不将其发送给docs所指示的其他小部件为止:

  

on_touch_down(触摸)在1.0.0版中添加

     

接收降落事件。

     

参数:

     

触摸: MotionEvent类收到触摸。

     

触摸位于父坐标中。有关坐标的讨论,请参见relativelayout   系统。

     

返回

     

bool如果为True,则将停止调度touch事件。   如果为False,则该事件将继续分配给其他   小部件树。

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

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。

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)进行验证。

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()