python - 单击 `TextInput` 对象后在 Kivy 中重新捕获键盘焦点

标签 python textbox focus kivy keyboard-events

以下代码创建一个 Kivy具有单个屏幕的应用 MainScreen,显示 LabelTextInput 对象。

如果您在应用程序首次加载时提供一些键盘输入,它会在控制台中打印出有关按下哪个键的信息。但是,如果您随后单击 TextInput 对象,键盘会重新聚焦,以便您可以在 TextInput 对象中键入。但是,一旦您从 TextInput 对象中单击返回,例如在标签或背景上,按更多键,不执行相同的控制台打印操作;键盘似乎没有从 TextInput 对象“散焦”。

如何在点击 TextInput 对象后重新获得键盘焦点?

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.textinput import TextInput

kv = '''ScreenManagement:
    MainScreen:

<MainScreen>:
    name: "main"
    BoxLayout:
        Label:
            text: "Label"
            font_size: 20
            size_hint: 0.2,0.1
        TextInput
            input_filter: 'float'
            font_size: 20
            hint_text: "Input Text"
            size_hint: 0.2,0.1'''

class MainScreen(Screen):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
        self._keyboard.bind(on_key_down=self._on_keyboard_down)

    def _keyboard_closed(self):
        self._keyboard.unbind(on_key_down=self._on_keyboard_down)
        self._keyboard = None

    def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
        print('INFO: The key', keycode, 'has been pressed')

        return True # return True to accept the key. Otherwise, it will be used by the system.

class ScreenManagement(ScreenManager):
    pass

class MainApp(App):
    def build(self):
        return Builder.load_string(kv)

if __name__ == "__main__":
    MainApp().run()

最佳答案

一个可能的解决方案是使用 on_touch_down 事件并在您按下不包含 TextInput 的部分时重新配置键盘:

class MainScreen(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.config_keyboard()

    def on_touch_down(self, touch):
        self.isTextInput = False

        def filter(widget):
            for child in widget.children:
                filter(child)
            if isinstance(widget, TextInput) and widget.collide_point(*touch.pos):
                self.isTextInput = True
                widget.on_touch_down(touch)

        filter(self)

        if not self.isTextInput and self._keyboard is None:
            self.config_keyboard()

    def config_keyboard(self):
        self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
        self._keyboard.bind(on_key_down=self._on_keyboard_down)

    def _keyboard_closed(self):
        self._keyboard.unbind(on_key_down=self._on_keyboard_down)
        self._keyboard = None

    def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
        print('INFO: The key', keycode, 'has been pressed')

        return True # return True to accept the key. Otherwise, it will be used by the system.

关于python - 单击 `TextInput` 对象后在 Kivy 中重新捕获键盘焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49399940/

相关文章:

python - 自动计算属性字段的 Django 过滤器查询

python - 使用 docker 的 cookiecutter-django 本地开发

python - 我怎样才能用lxml解析html

c# - 表单启动时在文本框中编辑位置插入符号 C#

jquery - iOS 错误?使用 jQuery 聚焦时无法更改选择中的文本

java - 为什么 requestFocusInWindow() 会失败?

python - 获取某个范围内的整数输入

WPF:自动完成文本框,...再次

jquery - 为什么会focusout();在 jQuery 中,焦点移出字段后不执行功能?

html - 为什么在浏览器中 150 + 150 不等于 300?