Guest User

Untitled

a guest
Jun 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. from tkinter import *
  2. import re
  3.  
  4. 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']
  5. #Some Random crap, don't judge
  6.  
  7. class AutocompleteEntry(Entry):
  8. def __init__(self, *args, **kwargs):
  9.  
  10. Entry.__init__(self, *args, **kwargs)
  11. self.lista = kwargs['lis']
  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. self.bind("<Return>",self.selection)
  21.  
  22. self.lb_up = False
  23.  
  24. def changed(self, name, index, mode):
  25. if self.var.get() == '':
  26. try:
  27. self.lb.destroy()
  28. self.lb_up = False
  29. except AttributeError:
  30. pass
  31. else:
  32. words = self.comparison()
  33. if words:
  34. if not self.lb_up:
  35. self.lb = Listbox()
  36. self.lb.bind("<Double-Button-1>", self.selection)
  37. self.lb.bind("<Right>", self.selection)
  38. self.lb.place(x=self.winfo_x(), y=self.winfo_y() + self.winfo_height())
  39. self.lb_up = True
  40.  
  41. self.lb.delete(0, END)
  42. for w in words:
  43. self.lb.insert(END, w)
  44. else:
  45. if self.lb_up:
  46. self.lb.destroy()
  47. self.lb_up = False
  48.  
  49. def selection(self, event):
  50. if self.lb_up:
  51. self.var.set(self.lb.get(ACTIVE))
  52. self.lb.destroy()
  53. self.lb_up = False
  54. self.icursor(END)
  55.  
  56. def up(self, event):
  57.  
  58. if self.lb_up:
  59. if self.lb.curselection() == ():
  60. index = '0'
  61. else:
  62. index = self.lb.curselection()[0]
  63. if index != '0':
  64. self.lb.selection_clear(first=index)
  65. index = str(int(index) - 1)
  66. self.lb.selection_set(first=index)
  67. self.lb.activate(index)
  68.  
  69. def down(self, event):
  70.  
  71. if self.lb_up:
  72. if self.lb.curselection() == ():
  73. index = '-1'
  74. else:
  75. index = self.lb.curselection()[0]
  76. if index != END:
  77. self.lb.selection_clear(first=index)
  78. index = str(int(index)+1)
  79. self.lb.selection_set(first=index)
  80. self.lb.activate(index)
  81.  
  82. def comparison(self):
  83. pattern = re.compile('.*' + str(self.var.get()).lower() + '.*')
  84. print(pattern)
  85. return [w for w in self.lista if re.match(pattern, str.lower(w))]
  86.  
  87.  
  88. if __name__ == '__main__':
  89. root = Tk()
  90. root.tk_setPalette(background='#ffffff')
  91.  
  92. entry = AutocompleteEntry(lis=lista, root)
  93. entry.grid(row=0, column=0)
  94. Button(text='nothing').grid(row=1, column=0)
  95. Button(text='nothing').grid(row=2, column=0)
  96. Button(text='nothing').grid(row=3, column=0)
  97.  
  98. root.mainloop()
Add Comment
Please, Sign In to add comment