How to use input to TextInput in kivy to get different output

守給你的承諾、 提交于 2021-02-11 13:54:10

问题


I wrote the following code.

#-*- coding: utf-8 -*-
from kivy.config import Config
from kivy.uix.button import Button
from functools import partial
Config.set('graphics', 'width', 300)
Config.set('graphics', 'height', 300)

from kivy.lang import Builder
Builder.load_string("""
<KeybindTestWidget>:
    BoxLayout:
        size: root.size
        orientation: 'vertical'

        TextInput:
            id: textinput1
            size_hint_y: 0.45
            text: ""
            on_focus: root.set_activeTextInput("textinput1")

        BoxLayout:
            size_hint_y: 0.1

        TextInput:
            id: textinput2
            size_hint_y: 0.45
            text: ""
            on_focus: root.set_activeTextInput("textinput2")
""")

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window

class KeybindTestWidget(Widget):
    def __init__(self, **kwargs):
        super(KeybindTestWidget, self).__init__(**kwargs)

        self.bufHotKeyTextinputName = ""

        #key bind
        self._keyboard = Window.request_keyboard(
            self._keyboard_closed, self, 'text')
        if self._keyboard.widget:
            pass
        self._keyboard.bind(on_key_down=self._on_keyboard_down)
        self._keyboard.bind(on_key_up=self._on_keyboard_up)

    def _keyboard_closed(self):
        pass

    def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
        self.ids[self.bufHotKeyTextinputName].text = keycode[1]
        return True

    def _on_keyboard_up(self, keyboard, keycode):
        return True

    def set_activeTextInput(self, textInputName, *args):
        self.bufHotKeyTextinputName = textInputName

class TestApp(App):
    def __init__(self, **kwargs):
        super(TestApp, self).__init__(**kwargs)

    def build(self):
        return KeybindTestWidget()

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

In the above code, if I type from the keyboard when TextInput is in focus, the text will change in response to the key input.

For example, if I press the space key on my keyboard, TextInput will show "spacebar".

However, there is one problem with the above code.

That's what happens when I press "a" on the keyboard, as in the image below, and TextInput shows "aa".

The _on_keyboard_down function is executed a little before the keyboard input, so it duplicates the input.

I've tried the TextInput option readonly, which makes the on_key_down unresponsive.

Is there any good solution?


回答1:


One way to do it, is to use the Clock to schedule the call after the TextInput has finished drawing its letter (which we will erase).
You will also need from functools import partial

    def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
        Clock.schedule_once(partial(self.clear_and_set, keycode))
        return True

    def clear_and_set(self, keycode, *args):
        self.ids[self.bufHotKeyTextinputName].text = keycode[1]


来源:https://stackoverflow.com/questions/61864451/how-to-use-input-to-textinput-in-kivy-to-get-different-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!