Advertisement
AceScottie

TKAutocomplete.py

Jul 19th, 2019
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.75 KB | None | 0 0
  1. from Tkinter import *
  2.  
  3. class AutoComplete():
  4.     def __init__(self, root):
  5.         self.root = root
  6.         self.root.bind("<Button-1>", self.mouseClick)
  7.         self.root.geometry("1000x600")
  8.         self.active_window = None
  9.         self.button = [None]*1000
  10.         self.input = None
  11.     def set_entry(self, event, text, stringvar):
  12.         stringvar.set(text)
  13.         self.close_overlay(Event())
  14.         self.input.icursor(len(text))
  15.     def set_button_colour(self, event, button, state):
  16.         if state:
  17.             button.configure(bg="#BBBBBB")
  18.         else:
  19.             button.configure(bg="#DDDDDD")
  20.     def mouseClick(self, event):
  21.         mous_over_button = False
  22.         if self.input != None:
  23.             for i in range(len(self.button)):
  24.                 if self.button[i] != None:
  25.                     if event.widget == self.button[i]:
  26.                         mous_over_button = True
  27.             if mous_over_button:
  28.                 pass
  29.             elif event.widget == self.input or event.widget == self.active_window:
  30.                 self.autocomplete(event, self.sv, self.options)
  31.             else:
  32.                 self.close_overlay(event)
  33.  
  34.     def autocomplete(self, event, sv, options):
  35.         intxt = sv.get()
  36.         valid = []
  37.         if len(intxt) >2 :
  38.             for opt in options:
  39.                 if(opt[0:len(intxt)].lower() == intxt.lower()):
  40.                     valid.append(opt)
  41.             if len(valid) > 0:
  42.                 xpos = self.input.winfo_rootx() - self.root.winfo_rootx()
  43.                 ypos = self.input.winfo_rooty() - self.root.winfo_rooty() + self.input.winfo_height()
  44.                 activeoverlay = self.overlay(self.root, width=self.input.winfo_width(), x=xpos, y=ypos)
  45.                 button_height = 2
  46.                 buttonFrame = Frame(activeoverlay, padx=2, pady=2, borderwidth=2, highlightbackground="black", relief="ridge")
  47.                 buttonFrame.pack(side=TOP, fill=BOTH, expand=1)
  48.                 for i in range(len(valid)):
  49.                     self.button[i] = Button(buttonFrame, text=valid[i], height=1, command=lambda e=Event(), t=valid[i], tv=sv: self.set_entry(e, t, sv), width=100, bg="#DDDDDD", fg="black", borderwidth=0, relief=None)
  50.                     self.button[i].pack(fill=X)
  51.                     self.button[i].bind("<Enter>", lambda e=Event(), b=self.button[i], s=True: self.set_button_colour(e, b, s))
  52.                     self.button[i].bind("<Leave>", lambda e=Event(), b=self.button[i], s=False: self.set_button_colour(e, b, s))
  53.                     main_height=self.button[i].winfo_height()
  54.                 for but in self.button:
  55.                     if but != None:
  56.                         but.update()
  57.                         lastbut = but
  58.                 self.active_window.configure(height=lastbut.winfo_height()*len(valid))
  59.                 self.active_window.update()
  60.             else:
  61.                 self.close_overlay(Event())
  62.         else:
  63.             self.close_overlay(Event())
  64.     def close_overlay(self, event):
  65.         if self.active_window != None:
  66.             self.button = [None]*1000
  67.             self.active_window.pack_forget()
  68.             self.active_window.destroy()
  69.             self.active_window = None
  70.     def overlay(self, over, width=100, x=0, y=0): ##the overlay to hold the selection options
  71.         if self.active_window != None:
  72.             self.close_overlay(Event())
  73.         self.active_window = Canvas(over, width=width, bg="white", borderwidth=2, relief="raised")
  74.         self.active_window.pack_propagate(False)
  75.         self.active_window.place(x=x, y=y)
  76.         self.root.update_idletasks()
  77.         self.root.update()
  78.         return self.active_window
  79.    
  80.     def autocomplete_Entry(self, holder, vals): ##the modified Widget
  81.         self.sv = StringVar()
  82.         self.options = vals
  83.         self.sv.trace("w", lambda name, index, mode, sv=self.sv, opt=self.options, e=Event(): self.autocomplete(e, sv, opt))
  84.         self.input=Entry(holder, textvariable=self.sv)
  85.         self.input.pack()
  86.         return self.input, self.sv
  87.    
  88.  
  89.  
  90.     ##Just for texting
  91.     def run(self):
  92.         self.window = Frame(self.root)
  93.         self.window.pack(fill=BOTH, expand=1) #
  94.         options = ["apple", "apple1", "apple2", "apple3", "apple4", "apple5", "apple6", "apple7", "banana", "pare", "peach", "strawberry", "melon"]
  95.         entry = self.autocomplete_Entry(self.window, options)
  96.         self.root.mainloop()
  97. if __name__ == "__main__":
  98.     root = Tk()
  99.     AutoComplete(root).run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement