tkinter popup and text processing for autocomplete

I'm making autocomplete feature for a text editor in tkinter & python. Currently the process of autocomplete is:

If there is a input like the one in a dictionary of autocomplete,call popup.

I do it via t_start.bind("< Key >", asprint) where asprint is my popup function. I can escape the popup via escape button or by clicking elsewhere. What I want is - upon user pressing any text key - re-trigger popup again, narrowing search in the autocomplete.

F->FI->FIL->FILE

sort of thing. I don't know what to use to get that input, AFTER the popup is open. How do I get 2nd and every following input character?

The popup function is:

def popup(event):
    selected_text=''
    try:
        selected_text=t_start.get("sel.first", "sel.last")
    except TclError:
        for i in range(len(selected_text)):
            if selected_text[i:0]==word[i:0]:
                menu.add_command(label="%s" %selected_text, command=insert_word)
                menu.delete(0) 
            else:
                pass
        menu.tk_popup(event.x_root, event.y_root) 

Answers


The key is to keep the keyboard focus in your entry widget. When you popup your window, make sure the focus stays (or is returned to) the entry widget. Any events that affect the popup need to be attached to the entry widget rather than the popup window.

However, if you're using a menu as your popup, this will be impossible. A menu is the wrong choice for an auto-complete feature because it steals all events until the menu is dismissed. Your popup needs to be an instance of a Toplevel widget (if you want it to "float") or some other widget (listbox, text, canvas, etc) if you want it embedded inside your window.

There is a recipe on ActiveState that gives an example of doing autocomplete using an embedded window. http://code.activestate.com/recipes/578253-an-entry-with-autocompletion-for-the-tkinter-gui/


Need Your Help

Javascript safely use user regex

javascript regex

I want to safely use regexp that the user inputs in order to parse text. I don't want to use a sandbox or anything. Can I just plug-in the regexp into String.match or would that cause problems? If ...

QtWebKit 2.2 segfaults when loading specific website

c++ qt qtwebkit

The past months I have been putting together my own QtWebKit-based browser (WildFox, BSD-licensed) and it's been an overall quite pleasant experience. I have noticed odd crashes however, basically ...