簡體   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