python - Problems with Tkinter "autocomplete" Entry widget -
i have been looking way add autocompletion tkinter ttk.entry widget. stumbled website suggested this solution. reading robb's answer, able make work regardless of case. kind of usage looking (adding new item sqlite database while getting autocompletion previous records), there weird pressing key. first version of code tried (from this link, ian weisser) delete character everytime user pressed . , version on stackoverflow had stripped lines of code catched <backspace>, <left>, <right>, <up>, <down>
keystrokes, think while typing, if user presses left, autocompletion should suspended, deleting selected text after cursor position.
so, how can change code user may enter text , go few characters correct misspelling, without deleting text?
i have edited code, based on 2 sources and, me, think works better:
def handle_keyrelease(self, event): """event handler keyrelease event on widget""" if event.keysym == "backspace": if self.position < self.index(end): # delete selection self.delete(self.position, end) else: #self.delete(self.index(insert), end) self.position = self.index(end) if event.keysym == "left": if self.position < self.index(end): # delete selection self.delete(self.position, end) #else: #self.position = self.position-1 # delete 1 character #self.delete(self.position, end) if event.keysym == "right": self.position = self.index(end) # go end (no selection) if event.keysym == "down": self.autocomplete(1) # cycle next hit if event.keysym == "up": self.autocomplete(-1) # cycle previous hit # perform normal autocomplete if event single key or umlaut if len(event.keysym) == 1: self.autocomplete()
basically, it's ian weisser's version. added if
clause "backspace" key part , removed above commented lines in order allow user go , correct misspelling. if user goes , edits something, rest of entered text intact , text gets deleted if user presses (deletes remaining autocompletion leaves rest intact) or backspace (deletes autocompletion , 1 character @ time, expected). if user mouse-clicks in text, nothing deleted.
Comments
Post a Comment