Kivy Read-Only TextInput, but with ability to select all (ctrl+a)

85 views Asked by At

How can I make a TextInput in a python GUI app built with kivy that's read-only, but where the user can still select all of the text with crtl+a?

Consider the following simplified example program with a TextInput containing over 9000 characters

import random

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput

class MyApp(App):

    def build(self):
        layout = BoxLayout()

        # generate some random ASCII content
        textinput_contents = ''.join( [chr( random.randint(32,127) ) for i in range(0,9001)] )

        # add the textinput
        self.textinput1 = TextInput(
         text=textinput_contents,
         readonly=True
        )
        layout.add_widget(self.textinput1)

        return layout

if __name__ == "__main__":
    MyApp().run()

I don't want the user to be able to edit the contents of the TextInput, but I do want them to be able to click around, select text, copy from it, etc. Unfortunaetly, in the above example, typing ctrl+a (for the Select All shortcut) does nothing.

However, if I omit the readonly attribute, then ctrl+a works again!

import random

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput

class MyApp(App):

    def build(self):
        layout = BoxLayout()

        # generate some random ASCII content
        textinput_contents = ''.join( [chr( random.randint(32,127) ) for i in range(0,9001)] )

        # add the textinput
        self.textinput1 = TextInput(
         text=textinput_contents
        )
        layout.add_widget(self.textinput1)

        return layout

if __name__ == "__main__":
    MyApp().run()

How can I make the kivy TextInput read-only without disabling Select-All (ctrl+a)?

0

There are 0 answers