Intro with suggestions

I am creating a small PyGTK application and I have an input text box (currently ComboBoxEntry) that is populated with several values ​​that the user should be able to select.

I think that I want to filter out the corresponding fields and show only those so that the user, using the keyboard arrows, can select one of the suitable ones.

To give some background, the predefined values ​​are a bunch of URLs, and the user should be able to select one or fill out a new one.

Example: predefined URLs:

When the user types ' http: //www.g ', the three URLs starting with this line should be shown (in some way) and when you enter ' http://www.goog ', starting from this will be shown

Any ideas?

+6
source share
4 answers

An Entrywith EntryCompletionseems more appropriate than ComboBoxEntry. As always, a good start tutorial .

It is very easy to configure when the list of predefined URLs is small and fixed. You just need to populate the ListStore:

# simplified example from the tutorial
import gtk

urls = [
    'http://www.google.com',
    'http://www.google.com/android',
    'http://www.greatstuff.com',
    'http://www.facebook.com',
    ]
liststore = gtk.ListStore(str)
for s in urls:
    liststore.append([s])

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

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

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

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

"http://" "www.", , , URL- (, "og"!):

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

ListStore , ( , 1000 ).

EntryCompletion, .

+7

, Tablebar Cuemiac .

0

, , , , , , - . , . , , (, , ), trie, , , node. , .

0

, " -", ( ): ( S P F...)
?? (python v2.7.15 + GTK3)

: '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)

# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Name:     AFAC_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   # , Gdk, GLib, GdkPixbuf, Pango

    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()
# -- END OF CODE ---
-1
source

All Articles