Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_autocomplete.py ZZZ
- from tkinter import *
- import re
- class AutocompleteEntry(Entry):
- def __init__(self, autocompleteList, *args, **kwargs):
- # Listbox length
- if 'listboxLength' in kwargs:
- self.listboxLength = kwargs['listboxLength']
- del kwargs['listboxLength']
- else:
- self.listboxLength = 8
- # Custom matches function
- if 'matchesFunction' in kwargs:
- self.matchesFunction = kwargs['matchesFunction']
- del kwargs['matchesFunction']
- else:
- def matches(fieldValue, acListEntry):
- pattern = re.compile('.*' + re.escape(fieldValue) + '.*', re.IGNORECASE)
- return re.match(pattern, acListEntry)
- self.matchesFunction = matches
- Entry.__init__(self, *args, **kwargs)
- self.focus()
- self.autocompleteList = autocompleteList
- self.var = self["textvariable"]
- if self.var == '':
- self.var = self["textvariable"] = StringVar()
- self.var.trace('w', self.changed)
- self.bind("<Return>", self.selection)
- self.bind("<Up>", self.moveUp)
- self.bind("<Down>", self.moveDown)
- self.listboxUp = False
- def changed(self, name, index, mode):
- if self.var.get() == '':
- if self.listboxUp:
- self.listbox.destroy()
- self.listboxUp = False
- else:
- words = self.comparison()
- if words:
- self.listboxItems = len(words)
- if not self.listboxUp:
- self.listbox = Listbox(width=self["width"], height=self.listboxLength)
- self.listbox.bind("<Double-Button>", self.selection)
- self.listbox.bind("<Return>", self.selection)
- self.listbox.place(x=self.winfo_x(), y=self.winfo_y() + self.winfo_height())
- self.listboxUp = True
- self.listbox.delete(0, END)
- for w in words:
- self.listbox.insert(END,w)
- self.show_active(0)
- else:
- if self.listboxUp:
- self.listbox.destroy()
- self.listboxUp = False
- def show_active(self, moveto):
- if self.listboxUp:
- try:
- index = self.listbox.curselection()[0]
- self.listbox.selection_clear(first=index)
- L = self.listboxItems
- index = str(min(L-1,max(0,int(index)+moveto)))
- except:
- index = '0'
- self.listbox.selection_set(first=index)
- self.listbox.activate(index)
- self.listbox.see(index) # Scroll!
- def selection(self, event):
- if self.listboxUp:
- root.clipboard_clear()
- item = self.listbox.get(ACTIVE)
- self.var.set(item)
- self.listbox.destroy()
- self.listboxUp = False
- self.icursor(END)
- root.clipboard_append(item)
- def moveUp(self, event):
- self.show_active(-1)
- def moveDown(self, event):
- self.show_active(+1)
- def comparison(self):
- return [ w for w in self.autocompleteList if self.matchesFunction(self.var.get(), w) ]
- autocompleteList = '''
- ### SEE pastebin(.)com/pmg0gHuU for this list
- '''.splitlines()
- def matches(fieldValue, acListEntry):
- pattern = re.compile(re.escape(fieldValue) + '.*', re.IGNORECASE)
- return re.match(pattern, acListEntry)
- root = Tk()
- ww = root.winfo_screenwidth()
- hh = root.winfo_screenheight()
- root.geometry("198x690+-3+-3")
- entry = AutocompleteEntry(autocompleteList, root, listboxLength=40, width=32, matchesFunction=matches)
- entry.grid(row=0, column=0)
- Label(text='Tk Auto-Complete').grid(column=0)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement