如何在奇异果中使用输入到TextInput以获得不同的输出 [英] How to use input to TextInput in kivy to get different output

查看:91
本文介绍了如何在奇异果中使用输入到TextInput以获得不同的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下代码.

#-*- 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()

在上面的代码中,如果我在TextInput处于焦点状态时从键盘键入内容,则文本将根据按键输入而改变.

例如,如果我按下键盘上的空格键,TextInput将显示空格键".

但是,上面的代码存在一个问题.

就是这样,当我按下键盘上的"a"时,如下图所示,TextInput显示"aa".

_on_keyboard_down函数在键盘输入之前执行了一点,因此它复制了输入.

我尝试将TextInput选项设置为只读,这会使on_key_down无响应.

有什么好的解决方法吗?

解决方案

一种方法是使用Clock安排呼叫 ,在TextInput完成绘制后字母(我们将删除).
您还需要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]
 

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?

解决方案

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]

这篇关于如何在奇异果中使用输入到TextInput以获得不同的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆