问题描述
我完成了这段代码,但我无法将自动完成条目打包在它始终显示在父窗口中的顶层中,我尝试更改位置并尝试获得最佳解决方案,但始终失败。
我希望在子窗口中有一个自动完成条目,它带有一个列表框,当您输入的单词与您输入的单词相同时。
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,**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("<Right>",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:
if not self.listBoxUp:
self.listBox = ListBox(
width=self["width"],height=self.listBoxLength)
self.listBox.bind("<Button-1>",self.selection)
self.listBox.bind("<Right>",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)
else:
if self.listBoxUp:
self.listBox.destroy()
self.listBoxUp = False
def selection(self,event):
if self.listBoxUp:
self.var.set(self.listBox.get(ACTIVE))
self.listBox.destroy()
self.listBoxUp = False
self.icursor(END)
def moveUp(self,event):
if self.listBoxUp:
if self.listBox.curselection() == ():
index = '0'
else:
index = self.listBox.curselection()[0]
if index != '0':
self.listBox.selection_clear(first=index)
index = str(int(index) - 1)
self.listBox.see(index) # Scroll!
self.listBox.selection_set(first=index)
self.listBox.activate(index)
def moveDown(self,event):
if self.listBoxUp:
if self.listBox.curselection() == ():
index = '0'
else:
index = self.listBox.curselection()[0]
if index != END:
self.listBox.selection_clear(first=index)
index = str(int(index) + 1)
self.listBox.see(index) # Scroll!
self.listBox.selection_set(first=index)
self.listBox.activate(index)
def comparison(self):
return [w for w in self.autocompleteList if self.matchesFunction(self.var.get(),w)]
if __name__ == '__main__':
autocompleteList = ['Dora Lyons (7714)','Hannah Golden (6010)','Walker Burns (9390)','Dieter Pearson (6347)','Allen Sullivan (9781)','Warren Sullivan (3094)','Genevieve Mayo (8427)','Igor Conner (4740)','Ulysses Shepherd (8116)','Imogene Bullock (6736)','Dominique Sanchez (949)','Sean Robinson (3784)','Diana Greer (2385)','Arsenio Conrad (2891)','Sophia Rowland (5713)','Garrett Lindsay (5760)','Lacy Henry (4350)','Tanek Conley (9054)','Octavia Michael (5040)','Kimberly Chan (1989)','Melodie Wooten (7753)','Winter Beard (3896)','Callum Schultz (7762)','Prescott Silva (3736)','Adena Crane (6684)']
def matches(fieldValue,acListEntry):
pattern = re.compile(re.escape(fieldValue) + '.*',re.IGnorECASE)
return re.match(pattern,acListEntry)
root = Tk()
entry = AutocompleteEntry(
autocompleteList,root,listBoxLength=6,width=32,matchesFunction=matches)
entry.grid(row=0,column=0)
Button(text='Python').grid(column=0)
root.mainloop()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)