Hello, How do I set the number of chars at TextInput, I want to limit 10 characters.

2,063 views
Skip to first unread message

노재화

unread,
Feb 23, 2019, 4:28:52 AM2/23/19
to Kivy users support
Hello dears.

I want to make Textinput which can typo only '10 characters'.

In a nutshell, limit length of TextInput as 10.

I retrieve the documents at TextInput section. but, I couldn't find about length of words at Textinput.

How can I do?

Thanks.

Elliot Garbus

unread,
Feb 23, 2019, 11:50:38 AM2/23/19
to kivy-...@googlegroups.com

Look at the filtering examples: https://kivy.org/doc/stable/api-kivy.uix.textinput.html?highlight=text%20input#module-kivy.uix.textinput

And do the filtering based on length.

 

I did a filter that does not allow characters that are not allowed in a filename, and puts up a warning message if the name is already in the library the user created:

class ValidFilenameInput(TextInput):
    invalid_set =
r"/\*:|'.?" + '"<>'

   
def
insert_text(self, substring, from_undo=False):
        patch_name = [c
for c in substring if c not in self.invalid_set]
        s =
''.join(patch_name)
       
return super().insert_text(s, from_undo=from_undo)

 

and create a widget in kivy

ValidFilenameInput:
    id: patch_name
    multiline: False
    hint_text: 'Name'
    size_hint: (None,None)
    size: (400,30)
    on_text: input_warning.text = 'Name is Already in Library, Change the Name' if self.text in app.mylib else ''

 

You could do something similar, but stop returning chars after the first 10 are input.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To post to this group, send email to kivy-...@googlegroups.com.
Visit this group at https://groups.google.com/group/kivy-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/2a36807f-2fff-40c9-baa7-e8acbcaaaa19%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

 

노재화

unread,
Feb 24, 2019, 9:12:30 PM2/24/19
to kivy-...@googlegroups.com
Dear Elliot Garbus.
Thanks. Your reply is helping to me a lot.

I made the code from you had gave me.

below is that.

class ValidFilenameInput(TextInput):
    
    count = 0

    def insert_text(self, substring, from_undo=False):
        patch_name = [c for c in substring if self.count < 10]
        s = "".join(patch_name)

        if self.count < 10:
            self.count += 1
        return super().insert_text(s, from_undo=from_undo)


Thanks to you repeatedly.

Have a good day!


2019년 2월 24일 (일) 오전 1:50, Elliot Garbus <elli...@cox.net>님이 작성:
You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/xTcDcm2eKEE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.

To post to this group, send email to kivy-...@googlegroups.com.
Visit this group at https://groups.google.com/group/kivy-users.

Elliot Garbus

unread,
Feb 24, 2019, 9:43:58 PM2/24/19
to kivy-...@googlegroups.com

I'm glad to hear it helped.

You could simplify your code, something like this should work.  (I have not tested this).  You do not need to examine every character.

def insert_text(self, substring, from_undo=False)
    if len(substring) < 10 :  #  if the length of the substring is less than 10, return the substring
            s = substring
    else:
            s = substring[0:10]  # is the substring length is 10 or greater, only return the first 10 characters
    return super().insert_text(s, from_undo=from_undo)

노재화

unread,
Feb 25, 2019, 6:18:18 AM2/25/19
to kivy-...@googlegroups.com
Dear Elliot Garbus.
Oh, your suggestion is very charm.
But, It doesn't work when I use your new code.
the substring returns one character not full contents of TextInput.

Anyway, you are so kind.
Take care.


2019년 2월 25일 (월) 오전 11:44, Elliot Garbus <elli...@cox.net>님이 작성:

jw

unread,
Feb 25, 2019, 10:31:29 PM2/25/19
to Kivy users support
I found another way to do this:

1. Create a class for the textinput

2. Create a handle in the on_text event that sets readonly to True if the len == 10

3. Create a method in the textinput class that you created called keyboard_on_key_up and check if the readonly is True and the key is backspace then set readonly to False and do_backspace()

like this:

(in kv file)

on_text:
    self.readonly = True if len(self.text.strip()) == 10 else False

(in python file)

class LimitInput(TextInput):
    def keyboard_on_key_up(self, keycode, text):
        if self.readonly and text[1] == "backspace":
            self.readonly = False
            self.do_backspace()

hope this helps!

-Jaivin

노재화

unread,
Feb 27, 2019, 6:35:56 AM2/27/19
to kivy-...@googlegroups.com
Dear  Jaivin.
Oh, your code is simple and great.
I test it. and It works!

Thanks and Have a good day!

2019년 2월 26일 (화) 오후 12:31, jw <74i...@gmail.com>님이 작성:

Jaivin Wylde

unread,
Feb 27, 2019, 5:24:25 PM2/27/19
to kivy-...@googlegroups.com
No problem!

One thing I forgot to mention is that this will only work on desktop, if you want it to work on mobile you would do something like this:

Create this method in the text input (instead of keyboard_on_key_up):

def insert_text(self, substring, from_undo=False):
    if not self.filled:
        return super(IdeaInput, self).insert_text(substring, from_undo=from_undo)

Create this in kv under the text input:

on_text:
    self.filled = True if len(self.text.strip()) >= 10 else False

노재화

unread,
Feb 28, 2019, 9:19:34 AM2/28/19
to kivy-...@googlegroups.com
Dear  Jaivin. 
Oh, you gave to me another advice about the mobile!
I haven't made the mobile app now, but Thanks your offer when I make the mobile app.

Best regards.


2019년 2월 28일 (목) 오전 7:24, Jaivin Wylde <74i...@gmail.com>님이 작성:
Reply all
Reply to author
Forward
0 new messages