kivy: TextInput focus problem

This is a copy of my post at Google Groups. Python 2.7.10 / kivy 1.9.0 In this code:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from kivy.lang import Builder

Builder.load_string('''
<Base>:
    Button:
        text: 'Press for Popup'
        on_press: root.edit_text()

<TextInputX>
    focus: True
''')


class Base(BoxLayout):
    def __init__(self, **kwargs):
        super(Base, self).__init__(**kwargs)

    def edit_text(self, *args):
        self.clear_widgets()
        content = TextInputX()
        # content.focus = True
        popup = Popup(content=content)
        popup.open()


class TextInputX(TextInput):
    def __init__(self, **kwargs):
        super(TextInputX, self).__init__(**kwargs)
        # self.focus = True


class SampleApp(App):
    def build(self):
        return Base()


if __name__ == '__main__':
    SampleApp().run()

when the popup with the TextInput opens, its impossible to type.

If you erase the “focus: True” from the kv part you can write, but you lose focus when the popup opens. If you uncomment either of the “content.focus = True” or “self.focus = True” from the py side, nothing happens. They act as “focus = False” And of course, if you put “focus: False” in the kv part, it works as expected (no focus).

So does anybody knows of a way to set the focus AND be able to type? TIA

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 36 (25 by maintainers)

Commits related to this issue

Most upvoted comments

@toniree There are many ways to solve this problem. The below, simplified code works in Kivy V2.

If you still have issues, rather than posting on an old ticket, please create a fresh ticket with all your version details, code and logs. Thanks

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from kivy.lang import Builder

Builder.load_string('''
<Base>:
    Button:
        text: 'Press for Popup'
        on_release: root.edit_text()
''')


class Base(BoxLayout):
    def edit_text(self, *args):
        content = TextInputX()
        popup = Popup(content=content)
        popup.open()


class TextInputX(TextInput):
    def on_parent(self, widget, value):
        self.focus = True


class SampleApp(App):
    def build(self):
        return Base()

if __name__ == '__main__':
    SampleApp().run()

Setting unfocus_on_touch to False is your friend when dealing with TextInputs.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from kivy.lang import Builder

Builder.load_string('''
<Base>:
    Button:
        text: 'Press for Popup'
        on_press: root.edit_text()
''')

class Base(BoxLayout):
    def __init__(self, **kwargs):
        super(Base, self).__init__(**kwargs)
    def edit_text(self, *args):
        self.clear_widgets()
        content = TextInputX()
        content.unfocus_on_touch = False
        content.focus = True
        popup = Popup(content=content)
        popup.open()

class TextInputX(TextInput):
    def __init__(self, **kwargs):
        super(TextInputX, self).__init__(**kwargs)
        # self.focus = True

class SampleApp(App):
    def build(self):
        return Base()

if __name__ == '__main__':
    SampleApp().run()

This thread seems to still be opened. Lets close it. Took me a long time to find the fix for solving the focus issue. Here is the place in the documentation for solving it: Focus Behavior