Событие on_touch_down
принимается всеми виджетами до тех пор, пока один из них не вернет True, сообщая циклу событий, что он его использует, и, следовательно, не отправит его другим виджетам, как указано docs :
on_touch_down (touch) Добавлено в 1.0.0
Получение события касания.
Параметры:
touch: Класс MotionEvent Touch получил.
Касание в родительских координатах. Смотрите относительное расположение для обсуждения координат
системы.
Возвращает :
bool Если True, отправка события касания остановится.
Если значение 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, но devestaja заключается в том, что вы не можете вернуть 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()