capitalize only the first letter in a text input area

126 views
Skip to first unread message

husam alkdary

unread,
Apr 8, 2020, 3:34:38 PM4/8/20
to Kivy users support
it's possible to capitalize only the first letter in a text input area using this method 

class CapitalInput(TextInput):
   
def insert_text(self, substring, from_undo=False):
        s
= substring.upper()
       
return super(CapitalInput, self).insert_text(s, from_undo=from_undo)

this function capitalize  all the text but I want only the first litter 

ZenCODE

unread,
Apr 8, 2020, 5:21:04 PM4/8/20
to Kivy users support
Try
    s = substring.title()

husam alkdary

unread,
Apr 9, 2020, 7:15:03 AM4/9/20
to Kivy users support
it's not work
the idea of this method is to make the changes instantaneity for every chart immediately when we typed in the widget
but my Idea is to make the textinput change immediately only the first litter ?
also may make the textinput widget request the upper case litter in the keyboard ? 

Elliot Garbus

unread,
Apr 9, 2020, 10:27:43 AM4/9/20
to kivy-...@googlegroups.com

class CapitalInput(TextInput):
   
def insert_text(self, substring, from_undo=False):

        s = substring.upper() if len(self.text) == 0 else substring
       
return super(CapitalInput, self).insert_text(s, from_undo=from_undo)

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/56c36997-9edc-4588-8084-325a67f830ee%40googlegroups.com.

 

husam alkdary

unread,
Apr 10, 2020, 6:05:54 PM4/10/20
to Kivy users support
first I modified you code to capitalize the first letter in each line 

class CapitalInput(TextInput):
   
def insert_text(self, substring, from_undo=False):

        s
= substring.upper() if self.cursor_col == 0 else substring
       
return super(CapitalInput, self).insert_text(s, from_undo=from_undo)


 

this works fine only if the interred value is string but if for example is space it will not capitalize the first letter after the space
what should I do to ignore that if is possible

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Elliot Garbus

unread,
Apr 10, 2020, 8:13:42 PM4/10/20
to kivy-...@googlegroups.com

Here you go:

 

class CapitalInput(TextInput):
   
def __init__(self, **kwargs):
       
self.new_line = False
       
super().__init__(**kwargs)

   
def insert_text(self, substring, from_undo=False):
       
if self.cursor_col == 0:
           
self.new_line = True
        if
self.new_line and substring.isspace():
            new_text = substring
       
elif self.new_line and not substring.isspace():
            new_text = substring.upper()
           
self.new_line = False
        else
:
            new_text = substring
       
return super().insert_text(new_text, from_undo=from_undo)

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/15ff4be2-105b-40ec-9934-c39f49424294%40googlegroups.com.

 

Robert Flatt

unread,
Apr 10, 2020, 9:46:08 PM4/10/20
to Kivy users support
What should I do if there are two spaces at the beginning of a line?  ;)

Elliot Garbus

unread,
Apr 10, 2020, 10:11:53 PM4/10/20
to kivy-...@googlegroups.com

Take a closer look 😊 it’s covered.

 

At col == 0, the new_line flag is set indicating we are on a new line.  

new_line  stays True until a non-white space character is encountered. There can be any number of leading white space characters.

 

There is a possible open issue.  If after you have typed a few lines, if you move the cursor into some leading spaces (not column 0) and start typing, it will not be capitalized.

 

An executable to play with:

 

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.textinput import TextInput


class CapitalInput(TextInput):
   
def __init__(self, **kwargs):
       
self.new_line = False
       
super().__init__(**kwargs)

   
def insert_text(self, substring, from_undo=False):
       
if self.cursor_col == 0:
           
self.new_line = True
        if
self.new_line and substring.isspace():
            new_text = substring
       
elif self.new_line and not substring.isspace():
            new_text = substring.upper()
           
self.new_line = False
        else
:
            new_text = substring
       
return super().insert_text(new_text, from_undo=from_undo)


if __name__ == '__main__':
    kv =
"""
BoxLayout:
    orientation: 'vertical'
    Label:
        text: 'Test Capital Input'
    CapitalInput:
        id: ci
        hint_text: 'Enter text'
    GridLayout:
        cols: 3
        Label:
            text: 'cursor: ' + str(ci.cursor)
        Label:
            text: 'cursor_col: ' + str(ci.cursor_col)
        Label:
            text: 'cursor_row: ' + str(ci.cursor_row)
        Label: # placeholder
        Label:
            text: 'cursor_pos: ' + str(ci.cursor_pos)
    """


class TITestApp(App):
   
def build(self):
       
return Builder.load_string(kv)


TITestApp().run()

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/bbabe8b9-7d8d-4d57-931c-6c3757ae40d7%40googlegroups.com.

 

Robert Flatt

unread,
Apr 10, 2020, 11:04:22 PM4/10/20
to Kivy users support
You are a star :)

Elliot Garbus

unread,
Apr 10, 2020, 11:08:51 PM4/10/20
to kivy-...@googlegroups.com

LOL

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/d95b54a0-31fa-41fc-95c1-e2701d6b0a82%40googlegroups.com.

 

husam alkdary

unread,
Apr 11, 2020, 4:30:06 AM4/11/20
to Kivy users support
what's the mean of this 
isspace() , new_line
I didn't find any in the kivy doc
!!

husam alkdary

unread,
Apr 11, 2020, 4:37:20 AM4/11/20
to Kivy users support
can the code to approved to deal with all not string type not only space 
maybe using type function


On Saturday, April 11, 2020 at 5:08:51 AM UTC+2, Elliot Garbus wrote:

Elliot Garbus

unread,
Apr 11, 2020, 11:58:14 AM4/11/20
to kivy-...@googlegroups.com

Isspace() is a python string method. https://docs.python.org/3/library/stdtypes.html#string-methods

new_line is an instance variable that I created.  This variable is created as in instance variable rather than a local variable because it must retain the value across multiple method calls.

Here is a tutorial on oop in python: https://realpython.com/python3-object-oriented-programming/

 

Here are some line by line comments:

 

class CapitalInput(TextInput):
   
def __init__(self, **kwargs):
       
self.new_line = False  # create an instance variable.  When new_lins is true it means we have found a new line, but not yet capitalized a letter.
       
super().__init__(**kwargs)

   
def insert_text(self, substring, from_undo=False):  # insert_text is called on every character entered
       
if self.cursor_col == 0:
           
self.new_line = True  #  if we are on col 0, set new_line True.  We are on the start of a new line
        if
self.new_line and substring.isspace(): 
            new_text = substring 
# if  new line is True and the substring is whitespace, enter the whitespace into the TextInput
       
elif self.new_line and not substring.isspace():
            new_text = substring.upper()  
# if new_line is true and the character is not whitespace, uppercase the character, and set new_line false
           
self.new_line = False               # new_line will stay false until col == 0
        else
:
            new_text = substring              
# if new_line is false, insert the substring into the TextInput.
       
return super().insert_text(new_text, from_undo=from_undo)

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/0d9f872c-b20c-4fcb-a71a-c612e0c0956e%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages