question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TextInput focus problem

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:36 (25 by maintainers)

github_iconTop GitHub Comments

1reaction
Zen-CODEcommented, Oct 15, 2020

@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()
1reaction
Matteljaycommented, Nov 21, 2018

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()
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native focus issue when having two TextInput elements
The issue is when pressing on one InputField , it's focusing on the input but the cursor stays on the previous focused InputField...
Read more >
Text Input problem on Focus - Starling Forum
When TextInput is focused and content is scrolled to top by keyboard to show input field the zone is calcuated wrong, so the...
Read more >
Having a problem with text input focus in android devices
Hi to all , I am having a problem with the text input. When the user click on the menu icon, the menu...
Read more >
[Android] First TextInput focus automatically blurred #30162
I've a little problem with the TextInput of React-Native library on Android. When I want to focus an input for the 1st time,...
Read more >
Focus on the Next TextInput when Next Keyboard Button is ...
When we trigger focus on a second input this causes a blur to fire on our First Name text input. This would cause...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found