0

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)
2
  • Alternatively, perhaps you can trigger a Button to defocus the TextInput even when multiline is set to false. Also with the same action you can change the Label's text (or you may bind that with the attr. text of TextInput). Commented Feb 2, 2022 at 19:00
  • I didn't even think about that, that will solve my problem! But I wonder if kivy actually supports key combinations like shift-enter or ctrl-enter, if not I guess I have to write another function then. Commented Feb 3, 2022 at 1:07

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

3 Comments

The TextInput.text is just what I need (really thanks a lot), I have been trying to find a way to "interact" with the text in TextInput with the thinking that it hasn't been updated yet... (kind of misunderstood). Now I just need to find a way to replace the "down the line" from enter to shift/ctrl + enter.
Also when I print "keycode" by hitting enter (basically I used "on_key_down" instead of "on_keyboard" and "keycode" instead of "key" like yours), it gave me the number 40.
Yes this is equivalent to scancode (which is not guaranteed for availablity according to document) on that same method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.