I believe you will need to create your own widget to accomplish this task.
(我相信您将需要创建自己的小部件来完成此任务。)
You can use an Entry widget combined with a listbox to do this. (您可以将Entry小部件与列表框结合使用来执行此操作。)
I have put some code together that shows this concept for you. (我整理了一些代码,为您展示了这个概念。)
The input is case sensitive. (输入区分大小写。)
The code is not perfect but you can tweak it to fit your needs. (该代码不是完美的,但是您可以对其进行调整以满足您的需求。)
import tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('420x200')
frm = tk.Frame(self)
self.var = tk.StringVar()
ent = tk.Entry(frm, textvariable=self.var, fg='black', bg='white')
lst = tk.Listbox(frm, bd=0, bg='white')
item_list = ('Big Dog', 'Big Cat', 'big bird', 'Small Bird', 'Small Fish', 'Little Insect', 'Long Snake')
ent.grid(sticky=tk.EW)
frm.grid(sticky=tk.NW)
def get_input(*args):
lst.delete(0, tk.END)
string = self.var.get()
if string:
for item in item_list:
if item.startswith(string):
lst.insert(tk.END, item)
lst.itemconfigure(tk.END, foreground="black")
for item in item_list:
if item.startswith(string):
lst.grid(sticky=tk.NSEW)
elif not lst.get(0):
lst.grid_remove()
else:
lst.grid_remove()
def list_hide(e=None):
lst.delete(0, tk.END)
lst.grid_remove()
def list_input(_):
lst.focus()
lst.select_set(0)
def list_up(_):
if not lst.curselection()[0]:
ent.focus()
list_hide()
def get_selection(_):
value = lst.get(lst.curselection())
self.var.set(value)
list_hide()
ent.focus()
ent.icursor(tk.END)
self.var.trace('w', get_input)
ent.bind('<Down>', list_input)
ent.bind('<Return>', list_hide)
lst.bind('<Up>', list_up)
lst.bind('<Return>', get_selection)
def main():
app = App()
app.mainloop()
if __name__ == '__main__':
main()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…