2

I am using python-2.7 and kivy.Someone help me that when i click into TextBox then how to select text using python or kivy code?

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
Nirdesh Kumawat
  • 386
  • 2
  • 16
  • 56

3 Answers3

2

You have to use on_touch_down next to select_all() as I show below:

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

In a similar way you can do it from 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
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
2

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
Nirdesh
  • 174
  • 2
  • 8
2

The solution is as follow. Please refer to the snippets, example and output for details.

  1. Use select_all() to select all of the text displayed in the TextInput. Note: If there is no text then nothing is selected.
  2. When TextInput is focused, selection is cancelled. Therefore, we delay the text selection using Clock.schedule_once()

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

Snippets

Python Script

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

kv file

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

Example

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)

Output

enter image description here

ikolim
  • 15,721
  • 2
  • 19
  • 29