开发者社区> 问答> 正文

Python中tkinter ttk组合框的下拉和展开

我正在尝试使用tkinter中的combobox创建一个搜索栏,在用户键入内容时可以看到combobox有扩展项,并能够展开组合框,请提供解决方案而不创建类。

# 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()

展开
收起
安忆333 2019-12-02 16:47:27 1639 0
1 条回答
写回答
取消 提交回答
  • 你需要创建自己的小部件来完成此任务。 可以将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() 
    
    2019-12-02 16:49:29
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载