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)s = substring.title()
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.
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)
To unsubscribe from this group and stop receiving emails from it, send an email 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.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/15ff4be2-105b-40ec-9934-c39f49424294%40googlegroups.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.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/bbabe8b9-7d8d-4d57-931c-6c3757ae40d7%40googlegroups.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.
isspace() , new_line
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/d95b54a0-31fa-41fc-95c1-e2701d6b0a82%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/d95b54a0-31fa-41fc-95c1-e2701d6b0a82%40googlegroups.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.