如何在组合框的输入功能中接受小写字符以完成搜索

时间:2019-06-13 14:54:17

标签: python-2.7 gtk3

此代码(下面)与comboboxentry不匹配所有完成 以最后两个项目为例:(S或P或F字符...)
(python v2.7.15 + GTK3)

# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Name:     module1_TEST
# Python :  2.7.15
#-------------------------------------------------------------------------------
def main():

    # -- Import Gtk3 packages
    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk, Gdk

    ldivers = [
        'http://www.google.com',
        'http://www.google.com/android',
        'http://www.greatstuff.com',
        'http://www.facebook.com',
        'S002.0002',
        '01-311P1015H5_F011650'
        ]
    liststore = Gtk.ListStore(str)
    for s in ldivers:
        liststore.append([s])

    completion = Gtk.EntryCompletion()
    completion.set_model(liststore)
    completion.set_text_column(0)

    entry = Gtk.Entry()
    entry.set_completion(completion)

    def match_anywhere(completeur, entrystr, iter, data):
        modelstr = completeur.get_model()[iter][0]
        return entrystr in modelstr
    completion.set_match_func(match_anywhere, None)

    # boilerplate
    window = Gtk.Window()
    window.add(entry)

    window.connect('destroy', lambda w: Gtk.main_quit())
    window.show_all()
    Gtk.main()

if __name__ == '__main__':
    main()

我提出了一个解决方案,对我的经验不足,深表歉意,我只是想帮助那些看起来有点像我的人:)

1 个答案:

答案 0 :(得分:0)

解决方案:大写字母要与返回单的交叉'modelstr.lower()'一起考虑...

    def match_anywhere(completeur, entrystr, iter, data):
        modelstr = completeur.get_model()[iter][0]
        return entrystr in modelstr.lower()
    completion.set_match_func(match_anywhere, None)