简体   繁体   English

Kivy TextInput自动补全。 如何使用kv文件获得相同的结果?

[英]Kivy TextInput autocompletion. How to get same result using kv file?

I would like to use a kv file for the TextInput in my code, but I don't know how to get the same result, how to translate this line of code: 我想在我的代码中为TextInput使用一个kv文件,但是我不知道如何获得相同的结果,以及如何翻译此行代码:

text_input.bind(text=self.action)

someone could help me? 有人可以帮助我吗?

from kivy.app import App
from kivy.lang import Builder
from textwrap import dedent

class MyApp(App):

    def action(self,instance,value):

        word_list = ["hello", "hi", "man", "girl"]

        self.root.suggestion_text = ''

        val = value[value.rfind(' ') + 1:]

        if not val:
            return
        try:
            word = [word for word in word_list
                    if word.startswith(val)][0][len(val):]
            if not word:
                return
            self.root.suggestion_text = word
        except IndexError:
            print('Index Error.')

    def build(self):
        text_input = Builder.load_string(dedent('''
            TextInput
        '''))
        text_input.bind(text=self.action)
        return text_input

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

You need to bind on_text event with the action method, using app keyword to refer to your MyApp instance ( app.action(args) ) and the “:” syntax: 您需要使用app关键字引用MyApp实例( app.action(args) )和“:”语法,将on_text事件与action方法绑定on_text

from kivy.app import App
from kivy.lang import Builder
from textwrap import dedent


class MyApp(App):

    def action(self,instance,value):
        word_list = ["hello", "hi", "man", "girl"]
        self.root.suggestion_text = ''
        val = value[value.rfind(' ') + 1:]

        if not val:
            return
        try:
            word = [word for word in word_list
                    if word.startswith(val)][0][len(val):]
            if not word:
                return
            self.root.suggestion_text = word
        except IndexError:
            print('Index Error.')

    def build(self):
        text_input = Builder.load_string(dedent('''
            TextInput:
                on_text: app.action(self, self.text)
        '''))
        return text_input

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM