Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

python - tkinter ttk。组合框下拉/展开并专注于文本(tkinter ttk.Combobox dropdown/expand and focus on text)

I am trying to create a searchbar using combobox in tkinter and I want that while the user is typing they can see the combobox expanded with values.

(我正在尝试使用tkinter中的combobox创建一个搜索栏,我希望在用户键入内容时可以看到combobox扩展了值。)

I am able to expand the combobox but the focus on combobox entry(textbox) is lost as the combo box values are expanded.

(我能够展开组合框,但是随着组合框值的扩展,对组合框条目(文本框)的关注消失了。)

I have also tried to set focus of textvariable after expanding the combobox values but still in vain.

(在扩展组合框值之后,我也尝试设置textvariable的焦点,但仍然徒劳。)

Please provide a solution without creating classes.

(请提供解决方案而不创建类。)

# 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()
  ask by Hamza Sajjad translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...