Reputation: 344
The idea is to validate a TextInput with Enter key or via a "Button"
Issue: Is there any way to run on_text_validate in TextInput: with a Button or Enter key (which also trigger the button) and down the line with shift-enter or ctrl-enter? Because I need to update the text in TextInput to my label since I can't press Enter because my multiline=True. Also is there any way to know if there are texts in TextInput, so the "validate button" will be enable and highlight when you type something in TextInput.
I tried to search on the internet but only can find 2 options, 1 is to bind keyboard, 2 is set multiline=False. I chose option1 and spent the whole day but still can not solve the issue since there are not many examples.
Edit: I added an example to make mine more clear.
.kv file
TextInput:
multiline: True # Down the line by hitting shift-enter/ctrl-enter instead of enter
on_text_validate: # I want to run this line by hitting enter or via a Button:
root.on_text_validate(self)
Upvotes: 0
Views: 1444
Reputation: 2908
By default when the touch is outside the TextInput
widget the TextInput
gets unfocused. So if you trigger some action from a button (outside the TextInput
) then it is enough to take care of things other than the focus
.
Still it's unclear to me exactly what you want to happen.
If you want to defocus the TextInput
from keyboard on hitting enter or any other key you can just bind keyboard to some callback and perform the required action from that callback.
Based on this assumption you have this (full) code with some extra tweaking:
from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
class TestApp(App):
def build(self):
Window.bind(on_keyboard = self.validate_input)
return Builder.load_string(
"""
BoxLayout:
orientation: "vertical"
spacing: "5dp"
Label:
id: lbl
TextInput:
id: textfield
hint_text: "Enter text here"
""")
def validate_input(self, window, key, *args, **kwargs):
textfield = self.root.ids.textfield
if key == 13 and textfield.focus: # The exact code-key and only the desired `TextInput` instance.
# textfield.do_undo() # Uncomment if you want to strip out the new line.
textfield.focus = False
self.root.ids.lbl.text = textfield.text
# textfield.text = "" # Uncomment if you want to make the field empty.
return True
TestApp().run()
Upvotes: 2