我正在尝试使用 tkinter 中的组合框创建一个搜索栏,我希望当用户键入时,他们可以看到用值展开的组合框。我可以扩展组合框,但随着组合框值的扩展,对组合框条目(文本框)的关注会丢失。我还尝试在扩展组合框值后设置文本变量的焦点,但仍然徒劳。 请提供解决方案而不创建类。
# tkinter modules
import tkinter as tk
import tkinter.ttk as ttk
win=tk.Tk()
searchVar = tk.StringVar()
searchVar.trace("w", lambda name, index, mode, searchVar=searchVar:
on_searchText_edit())
searchBar=ttk.Combobox(win,values=["1","2","3"],width=50,textvar=searchVar)
searchBar.grid(row=0,column=1,columnspan=2,padx=5,pady=5)
def on_searchText_edit():
txt=searchBar.focus_get() #searchVar has the focus
searchBar.event_generate('<Down>')
txt.focus_set()
最佳答案
我相信您需要创建自己的小部件来完成此任务。 您可以使用 Entry 小部件与列表框结合来执行此操作。我已经整理了一些代码来向您展示这个概念。输入区分大小写。该代码并不完美,但您可以对其进行调整以满足您的需求。
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()
关于python - tkinter ttk.Combobox 下拉/展开并聚焦于文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59119896/