繁体   English   中英

Kivy:使用“on focus”或“on_touch_down”清除文本输入

[英]Kivy: Clearing Text Input with 'on focus' or 'on_touch_down'

我想清除TextInputtext:当我点击它时。 示例代码:

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但这也没有用。 我错过了什么? 先感谢您!

on_touch_down事件由所有小部件接收,直到返回 True 告诉事件循环它正在使用它,因此不会将其发送给其他小部件,如文档所示

on_touch_down(touch)添加于 1.0.0

接收触地事件。

参数:

触摸: MotionEvent 类触摸接收。

触摸在父坐标中。 有关坐标系的讨论,请参阅 relativelayout。

回报

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关联的事件,它使用self.collide_point(*touch.pos)覆盖on_touch_down验证。

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

您需要做的就是添加以下内容:

on_focus: self.text = '' if args[1] else self.text

on_focus是一个在选择或取消选择TextInput时调用的函数,该函数为您提供两个参数instance ,即选择或取消选择的内容,以及value ,即如果该instance被选择或取消选择,这两个参数将放在列表称为args ,因为我们在kv文件中的小部件本身中执行此操作,所以我们不必担心instance ,所以我们检查value是否为True ,如果是,则意味着TextInput被点击,所以我们清除它,否则我们将它设置为自身。

所以这就是脚本的样子:

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 = '' if args[1] else self.text
            TextInput:
                text: 'Write Your Last Name'
                on_focus: self.text = '' if args[1] else self.text
            TextInput:
                text: 'Write Your Phone Number'
                on_focus: self.text = '' if args[1] else self.text
"""

class MyApp(App):
    def build(self):
        root_widget = Builder.load_string(kv_string)
        return root_widget

if __name__ == "__main__":
    MyApp().run()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM