Skip to content

Instantly share code, notes, and snippets.

@bhaskar-nair2
Created June 23, 2018 09:24
Show Gist options
  • Save bhaskar-nair2/17c922b317c63bc8821dddd29f7f6e67 to your computer and use it in GitHub Desktop.
Save bhaskar-nair2/17c922b317c63bc8821dddd29f7f6e67 to your computer and use it in GitHub Desktop.
A tkinter code to make auto-filling entries.
from tkinter import *
import re
lista = ['i', 'went', 'through', 'executemany()', 'as', 'well', 'but', 'sorry', 'to', 'say', 'I', 'am', 'quite', 'new', 'to', 'both', 'python', 'and', 'sqlite.', 'My', 'doubt', 'is', 'like', 'if', 'I', 'have,', 'say', 'a', 'list', 'of', 'filenames', 'which', 'I', 'have', 'prepared', 'by', 'appending', 'values', 'inside', 'a', 'list', 'object,', 'then', 'how', 'can', 'I', 'use', 'it', 'with', 'executeMany..', 'is', 'it', 'like', 'simply', 'passing', 'the', 'list', 'object', '(say', 'fileList)', 'to', 'executemany()?..', 'Any', 'code', 'snippet', 'would', 'be', 'really', 'appreciated..', 'thanks']
#Some Random crap, don't judge
class AutocompleteEntry(Entry):
def __init__(self, *args, **kwargs):
Entry.__init__(self, *args, **kwargs)
self.lista = kwargs['lis']
self.var = self["textvariable"]
if self.var == '':
self.var = self["textvariable"] = StringVar()
self.var.trace('w', self.changed)
self.bind("<Right>", self.selection)
self.bind("<Up>", self.up)
self.bind("<Down>", self.down)
self.bind("<Return>",self.selection)
self.lb_up = False
def changed(self, name, index, mode):
if self.var.get() == '':
try:
self.lb.destroy()
self.lb_up = False
except AttributeError:
pass
else:
words = self.comparison()
if words:
if not self.lb_up:
self.lb = Listbox()
self.lb.bind("<Double-Button-1>", self.selection)
self.lb.bind("<Right>", self.selection)
self.lb.place(x=self.winfo_x(), y=self.winfo_y() + self.winfo_height())
self.lb_up = True
self.lb.delete(0, END)
for w in words:
self.lb.insert(END, w)
else:
if self.lb_up:
self.lb.destroy()
self.lb_up = False
def selection(self, event):
if self.lb_up:
self.var.set(self.lb.get(ACTIVE))
self.lb.destroy()
self.lb_up = False
self.icursor(END)
def up(self, event):
if self.lb_up:
if self.lb.curselection() == ():
index = '0'
else:
index = self.lb.curselection()[0]
if index != '0':
self.lb.selection_clear(first=index)
index = str(int(index) - 1)
self.lb.selection_set(first=index)
self.lb.activate(index)
def down(self, event):
if self.lb_up:
if self.lb.curselection() == ():
index = '-1'
else:
index = self.lb.curselection()[0]
if index != END:
self.lb.selection_clear(first=index)
index = str(int(index)+1)
self.lb.selection_set(first=index)
self.lb.activate(index)
def comparison(self):
pattern = re.compile('.*' + str(self.var.get()).lower() + '.*')
print(pattern)
return [w for w in self.lista if re.match(pattern, str.lower(w))]
if __name__ == '__main__':
root = Tk()
root.tk_setPalette(background='#ffffff')
entry = AutocompleteEntry(lis=lista, root)
entry.grid(row=0, column=0)
Button(text='nothing').grid(row=1, column=0)
Button(text='nothing').grid(row=2, column=0)
Button(text='nothing').grid(row=3, column=0)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment