Python and Kivy: how to select and copy all text from a Text Input

Hello!

I'm trying to learn Kivy...

1) I would like to select all text within a Text Input in Kivy. which attribute should I study to change my code myself?;

2) after selecting all the text, the copy button should copy the text. which attribute should I study to change my code myself?;

3) Subject part: how to make accented characters recognized? I don't even know how to ask to ask them to tell me what I should research to fix this.

Example file: main.py

from kivy.app import App
class MainApp(App):
    def build(self):
        self.title = 'Meu primeiro app'
if __name__ == '__main__':
    MainApp().run()

Example file: main.kv

FloatLayout:
    Label:
        text: "Título do meu app"
        font_size: '60sp'
        top: 350
    TextInput:
        text: "Escreva aqui seu texto"
        height: "350px"
        width: 350
        top: 500
        size_hint: None, None
        pos_hint: {'center_x': 0.5,}
    Button:
        text: "Botão 1"
        height: 40
        width: 120
        top: 140
        size_hint: None, None
        pos_hint: {'center_x': 0.4,}
    Button:
        text: "Botão 2"
        height: 40
        width: 120
        top: 140
        size_hint: None, None
        pos_hint: {'center_x': 0.6,}
    Button:
        text: "Selecionar"
        height: 40
        width: 120
        top: 95
        size_hint: None, None
        pos_hint: {'center_x': 0.4,}
    Button:
        text: "Copiar"
        height: 40
        width: 120
        top: 95
        size_hint: None, None
        pos_hint: {'center_x': 0.6,}
Author: Wilson Junior, 2019-08-23

1 answers

Well, welcome to the kivy world!

1) I would like to select all text within a Text Input in Kivy. Which attribute should I study for myself to change my code?;

This is possible with the textinput method select_all() or use the shortcut ctrl + a. If you want to copy only selected text within TextInput, use the selection_text() method. Example:

BoxLayout:
    orientation: 'vertical'
    TextInput:
        id: texto
        hint_text: "Escreva aqui seu texto"
    Button:
        text: 'Selecionar'
        on_release:
            texto.select_all()

2) after selecting all the text, the copy button should copy the text. Which attribute should I study for myself to change my code?;

Use the TextInput method copy(data='') or use the shortcut ctrl + c. With the copy method(data=") you can pass a text as an assignment to the data copy parameter (data = 'some text here') or else if you do not pass anything, it will copy everything that is selected into your TextInput. Example:

Button:
    text: 'Copiar'
    on_release:
        texto.copy()

3) Subject part: how to make accented characters recognized? I don't even know how to ask to ask them to tell me what I should research to fix this.

This is possible by loading the kivy script script.kv by Builder.load_string() modifying the encoding to utf-8. Example:

Home.kv

<Home>:
    orientation: 'vertical'
    Label:
        text: 'ÁLGÙÑ TÊXTÕ CÓM ÀCENTÛAÇÃO!!!'

Home.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout

class Home(BoxLayout):
    pass

with open('home.kv', 'r', encoding = 'utf8') as screen:
    Builder.load_string(screen.read())

class MainApp(App):
    def build(self):
        return Home()

MainApp().run()

To finish, follow a synthesized example with all your questions:

Main.py

from kivy.app import App  
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout

for nome in ['home']: # Dessa maneira você consegue chamar mais telas
    with open(f'{nome}.kv', 'r', encoding = 'utf8') as tela:
        Builder.load_string(tela.read())

class Home(BoxLayout): pass

class MainApp(App):
    def build(self):
        return Home()

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

Home.kv

<Button>:
    font_size: sp(30)

<Home>:
    orientation: 'vertical'
    TextInput:
        id: texto
        size_hint: 1, .8
        hint_text: "Escreva aqui um textão!"
        font_size: sp(30)
    BoxLayout:
        size_hint: 1, .2
        Button:
            text: 'Selecionar'
            on_release:
                texto.select_all()
        Button:
            text: 'Copiar'
            on_release:
                texto.copy()
        Button:
            text: 'Colar'
            on_release:
                texto.paste()

Animation of Example:

GIF of the program working.

References:

 2
Author: Victor Manhani, 2020-03-06 20:35:10