Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.Kivy. Is there any way to limit entered text in TextInput widget?

Tags:

python

kivy

I'm writing kivy app and resently I faced with a problem of unlimited inputing text in TextInput widget. Is there any solution to this problem?

like image 556
Alex Delarge Avatar asked Feb 25 '18 17:02

Alex Delarge


1 Answers

A possible solution is to create a new property and overwrite the insert_text method:

from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.properties import NumericProperty


class MyTextInput(TextInput):
    max_characters = NumericProperty(0)
    def insert_text(self, substring, from_undo=False):
        if len(self.text) > self.max_characters and self.max_characters > 0:
            substring = ""
        TextInput.insert_text(self, substring, from_undo)

class MyApp(App):
    def build(self):
        return MyTextInput(max_characters=4)


if __name__ == '__main__':
    MyApp().run()
like image 170
eyllanesc Avatar answered Nov 09 '22 22:11

eyllanesc