Kivy文本输入text_suggestion不可见

时间:2019-02-26 18:54:04

标签: python kivy textinput

我使用了在这里找到的代码:Suggestion text

1)问题是当值和列表中的现有单词(word_list)完全匹配时,我看不到文本输入。

2)而且,当我开始输入与一个现有单词相似的内容,然后输入不在该现有单词中的字母时,看不到文本输入。

示例: 1)我手动写了“棕色”,我看不到输入 2)我手动写了“兄弟”,我看不到输入。如果我输入“ brod”和其他任何字母,我都会看到文本输入。

我的代码:

class FirstNameTextInput(TextInput):

    cur2 = con.cursor()
    sql_first_name = cur2.execute('SELECT firstName from `employee`')
    first_name_employee = [str(t[0]) for t in sql_first_name]

    word_list = ('The quick brown fox jumps over the lazy old dog').split(' ')

    def on_text(self, instance, value):
        self.suggestion_text = ''
        first_name_employee = list(set(
        self.first_name_employee + value[:value.rfind(' ')].split(' ')))

        val = value[value.rfind(' ') + 1:]
        if not val:
            return
        try:
            # grossly inefficient just for demo purposes
            word = [word for word in first_name_employee
                if word.startswith(val)][0][len(val):]
            if not word:
                return
            self.suggestion_text = word
        except IndexError:
            print('Index Error.')

    def keyboard_on_key_down(self, window, keycode, text, modifiers):
        if self.suggestion_text and keycode[1] == 'enter':
            self.insert_text(self.suggestion_text + ' ')
            return True

        return super(FirstNameTextInput, self).keyboard_on_key_down(window, keycode, text, modifiers)

我的kv文件:

<AddAttendantPopup>:
text: 'Add Attendant'
size_hint: (0.8, 0.8)
auto_dismiss: True
first_name_input_text: first_name
last_name_input_text: last_name
email_address_input_text: email_address
lb_error: er
BoxLayout:
    orientation: 'vertical'
    Label:
        text: 'First Name'
    FirstNameTextInput:
        id: first_name
        multiline: False
        readonly: False
    Label:
        text: 'Last Name'
    TextInput:
        id: last_name
        multiline: False
    Label:
        text: 'Email Address'
    TextInput:
        id: email_address
        multiline: False
    BoxLayout:
        size_hint_y: None
        height: "40dp"
        Button:
            text: 'Cancel'
            on_press: root.dismiss_popup(0.1)
        Button:
            text: 'Add attendant'
            on_press: root.add_attendant_meeting()
    Label:
        id: er
        foreground_color: 1, 250, 100, 1
        color: 1, 0.67, 0, 1
        size_hint_y: None
        height: 0
        text: root.error

0 个答案:

没有答案