auto fill in entry in tkinter

Solutions on MaxInterview for auto fill in entry in tkinter by the best coders in the world

showing results for - "auto fill in entry in tkinter"
Kitty
28 Nov 2019
1from Tkinter import *
2import re
3
4lista = ['a', 'actions', 'additional', 'also', 'an', 'and', 'angle', 'are', 'as', 'be', 'bind', 'bracket', 'brackets', 'button', 'can', 'cases', 'configure', 'course', 'detail', 'enter', 'event', 'events', 'example', 'field', 'fields', 'for', 'give', 'important', 'in', 'information', 'is', 'it', 'just', 'key', 'keyboard', 'kind', 'leave', 'left', 'like', 'manager', 'many', 'match', 'modifier', 'most', 'of', 'or', 'others', 'out', 'part', 'simplify', 'space', 'specifier', 'specifies', 'string;', 'that', 'the', 'there', 'to', 'type', 'unless', 'use', 'used', 'user', 'various', 'ways', 'we', 'window', 'wish', 'you']
5
6
7class AutocompleteEntry(Entry):
8    def __init__(self, lista, *args, **kwargs):
9        
10        Entry.__init__(self, *args, **kwargs)
11        self.lista = lista        
12        self.var = self["textvariable"]
13        if self.var == '':
14            self.var = self["textvariable"] = StringVar()
15
16        self.var.trace('w', self.changed)
17        self.bind("<Right>", self.selection)
18        self.bind("<Up>", self.up)
19        self.bind("<Down>", self.down)
20        
21        self.lb_up = False
22
23    def changed(self, name, index, mode):  
24
25        if self.var.get() == '':
26            self.lb.destroy()
27            self.lb_up = False
28        else:
29            words = self.comparison()
30            if words:            
31                if not self.lb_up:
32                    self.lb = Listbox()
33                    self.lb.bind("<Double-Button-1>", self.selection)
34                    self.lb.bind("<Right>", self.selection)
35                    self.lb.place(x=self.winfo_x(), y=self.winfo_y()+self.winfo_height())
36                    self.lb_up = True
37                
38                self.lb.delete(0, END)
39                for w in words:
40                    self.lb.insert(END,w)
41            else:
42                if self.lb_up:
43                    self.lb.destroy()
44                    self.lb_up = False
45        
46    def selection(self, event):
47
48        if self.lb_up:
49            self.var.set(self.lb.get(ACTIVE))
50            self.lb.destroy()
51            self.lb_up = False
52            self.icursor(END)
53
54    def up(self, event):
55
56        if self.lb_up:
57            if self.lb.curselection() == ():
58                index = '0'
59            else:
60                index = self.lb.curselection()[0]
61            if index != '0':                
62                self.lb.selection_clear(first=index)
63                index = str(int(index)-1)                
64                self.lb.selection_set(first=index)
65                self.lb.activate(index) 
66
67    def down(self, event):
68
69        if self.lb_up:
70            if self.lb.curselection() == ():
71                index = '0'
72            else:
73                index = self.lb.curselection()[0]
74            if index != END:                        
75                self.lb.selection_clear(first=index)
76                index = str(int(index)+1)        
77                self.lb.selection_set(first=index)
78                self.lb.activate(index) 
79
80    def comparison(self):
81        pattern = re.compile('.*' + self.var.get() + '.*')
82        return [w for w in self.lista if re.match(pattern, w)]
83
84if __name__ == '__main__':
85    root = Tk()
86
87    entry = AutocompleteEntry(lista, root)
88    entry.grid(row=0, column=0)
89    Button(text='nothing').grid(row=1, column=0)
90    Button(text='nothing').grid(row=2, column=0)
91    Button(text='nothing').grid(row=3, column=0)
92
93    root.mainloop()