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

Asked

Viewed 503 times

0

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 for part: how to ensure that accented characters are recognised? I don’t even know how to ask to be told what I should research to fix this.

File example: main py.

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

File example: 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,}
  • The question is clear and objective. About the code I agree that could be more complete, however, I preferred to present only this part, because it is better to present something clean than a mess that even I was not understanding. I don’t need you to touch the code. I just need an indication: which attribute to select the text I should study? You can make a statement?

  • I have even tried using the example of this page ---> https://coredump.pt/questions/50433534/python-kivy-how-to-select-a-textinput-without-mouse-click <--- but it does not work. I’ve even tried to play it like it is, but the window is all black.

1 answer

2

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 method select_all() from Textinput or use the shortcut ctrl + a. If you want to copy only a selected text inside Textinput, use the method selection_text(). 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 to change my code myself?;

Use the method copy(data='') from Textinput or use the shortcut ctrl + c. With the copy(data='') method you can pass a text as an assignment to the data copy(data = 'some text here') parameter or if you don’t pass anything, it will copy everything that is selected in your Textinput. Example:

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

3) Subject for part: how to ensure that accented characters are recognised? I don’t even know how to ask to be told what I should research to fix this.

This is possible by loading the kivy script script.kv for 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()

Finally, here is a summary of 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 the Example:

Gif do programa funcionando.

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.