Woody Pride
Woody Pride

Reputation: 13955

Automatic 'focus' of TextInput Kivy

I have a textinput widget that looks like this:

<ReaderWidget>:
    Label:
        text: 'Please scan EBT card'
        font_size: root.height/8
        size: self.texture_size
        bold: True
        color: 0, 0.70, 0.93, 1
    TextInput:
        focus: True
        password: True
        multiline: False
        cursor: 0, 0

The widget is dynamically added to the layout based on the user pressing a button in another widget. Currently the user has to point the mouse/finger into the text box before entering text, and I want the cursor to be in the text box ready to receive text without the user having to indicate by mouse press. Is there a way to do this?

It seems like focus : True should do it. But it doesn't seem to.

Upvotes: 8

Views: 10235

Answers (3)

Matthias Schreiber
Matthias Schreiber

Reputation: 2527

I had the same issue, but not for a button, so the on_release was not an option. If you want to do it with the on_touch_down method, you can focus the widget and add the current touch to be ignored for focusing:

def on_touch_down(self, touch):
    self.focus = True
    FocusBehavior.ignored_touch.append(touch)

Of course you also need to import FocusBehavior:

from kivy.uix.behaviors.focus import FocusBehavior

You can read more here: https://kivy.org/doc/stable/api-kivy.uix.behaviors.focus.html

Upvotes: 0

intrepidhero
intrepidhero

Reputation: 701

I know this is old but I found this question when I was trying to do something very similar. My code for adding the TextInput (and setting it's focus) was in the on_press handler for a button. A press would cause the TextInput to be added to the layout and it's focus set, but then it would lose focus when the button was released. Moving my code to on_release fixed the problem.

Upvotes: 6

Peter
Peter

Reputation: 1432

This worked for me in kivy 1.9.0:

def show_keyboard(event):
    text_input.focus = True
Clock.schedule_once(show_keyboard)

If text_input.focus is set directly, it doesn't seem to work. Perhaps this is a bug in kivy?

Upvotes: 2

Related Questions