如何 select TextBox 中的文本

How to select text in TextBox

我正在使用 python-2.7kivy。有人帮助我,当我点击进入 TextBox 然后如何使用 python 或 kivy select 文本代码?

test.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (400, 50)

class abc(BoxLayout):
    pass

class Test(App):
    def build(self):
        return abc()


if __name__ == '__main__':
    Test().run()

test.kv

<abc>:
    BoxLayout:
        orientation: "vertical"
        size_hint_y: .5

        BoxLayout:
            orientation: "horizontal"
            spacing: 10, 10
            size_hint_x: .6

            Label:
                text: "TEXT"
                text_size: self.size
                valign: 'middle'
                size_hint_x: .2

            TextInput:
                size_hint_x: .4

您必须在 select_all() 旁边使用 on_touch_down,如下所示:

#:import Clock kivy.clock.Clock

<abc>:
    BoxLayout:
        orientation: "vertical"
        size_hint_y: .5

        BoxLayout:
            orientation: "horizontal"
            spacing: 10, 10
            size_hint_x: .6

            Label:
                text: "TEXT"
                text_size: self.size
                valign: 'middle'
                size_hint_x: .2

            TextInput:
                size_hint_x: .4
                on_touch_down: Clock.schedule_once(lambda dt: self.select_all())

以类似的方式,您可以从 python 开始。

*.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
from kivy.uix.textinput import TextInput
from kivy.clock import Clock

Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (400, 50)

class abc(BoxLayout):
    pass

class MyTextInput(TextInput):
    def on_touch_down(self, touch):
        Clock.schedule_once(lambda dt: self.select_all())
        TextInput.on_touch_down(self, touch)

class Test(App):
    def build(self):
        return abc()


if __name__ == '__main__':
    Test().run()

*.kv

<abc>:
    BoxLayout:
        orientation: "vertical"
        size_hint_y: .5

        BoxLayout:
            orientation: "horizontal"
            spacing: 10, 10
            size_hint_x: .6

            Label:
                text: "TEXT"
                text_size: self.size
                valign: 'middle'
                size_hint_x: .2

            MyTextInput:
                size_hint_x: .4

test.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
from kivy.uix.textinput import TextInput
from kivy.clock import Clock

Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (400, 50)

class abc(BoxLayout):
    pass

class MyTextInput(TextInput):
    def on_focus(self, instance, isFocused):
        if isFocused:
            Clock.schedule_once(lambda dt: self.selected_text())

    def selected_text(self):
        ci = self.cursor_index()
        cc = self.cursor_col
        line = self._lines[self.cursor_row]
        len_line = len(line)
        start = max(0, len(line[:cc]) - line[:cc].rfind(u' ') - 1)
        end = line[cc:].find(u' ')
        end = end if end > - 1 else (len_line - cc)
        Clock.schedule_once(lambda dt: self.select_text(ci - start, ci + end))


class Test(App):
    def build(self):
        return abc()


if __name__ == '__main__':
    Test().run()

test.kv

<abc>:
    BoxLayout:
        orientation: "vertical"
        size_hint_y: .5

        BoxLayout:
            orientation: "horizontal"
            spacing: 10, 10
            size_hint_x: .6

            Label:
                text: "TEXT"
                text_size: self.size
                valign: 'middle'
                size_hint_x: .2

            MyTextInput:
                size_hint_x: .4

解决方法如下。有关详细信息,请参阅代码段、示例和输出。

  1. 使用select_all() 可以select TextInput 中显示的所有文本。 注意:如果没有文本,则不会 selected。
  2. TextInput 被聚焦时,selection 被取消。因此,我们使用 Clock.schedule_once()
  3. 延迟文本 selection

Text Input » API » Note

Selection is cancelled when TextInput is focused. If you need to show selection when TextInput is focused, you should delay (use Clock.schedule) the call to the functions for selecting text (select_all, select_text).

片段

Python 脚本

def on_focus(self, instance):
    if instance.focus:
        Clock.schedule_once(lambda dt: instance.select_all())

kv 文件

        TextInput:
            size_hint_x: .4
            on_focus: root.on_focus(self)

例子

main.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
from kivy.clock import Clock

Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (400, 50)


class abc(BoxLayout):
    def on_focus(self, instance):
        if instance.focus:
            print("TextInput is focused [focus={}]".format(instance.focus))
            Clock.schedule_once(lambda dt: instance.select_all())
        else:
            print("TextInput is defocused [focus={}]".format(instance.focus))


class Test(App):
    def build(self):
        return abc()


if __name__ == '__main__':
    Test().run()

test.kv

#:kivy 1.11.0

<abc>:
    BoxLayout:
        orientation: "vertical"
        size_hint_y: .5

        BoxLayout:
            orientation: "horizontal"
            spacing: 10, 10
            size_hint_x: .6

            Label:
                text: "TEXT"
                text_size: self.size
                valign: 'middle'
                size_hint_x: .2

            TextInput:
                size_hint_x: .4
                on_focus: root.on_focus(self)

输出