使用tkinter编辑文本时显示下拉列表组合框 [英] Show combobox drop down while editing text using tkinter

查看:57
本文介绍了使用tkinter编辑文本时显示下拉列表组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打开下拉菜单时是否可以使Combobox可编辑?我还没有找到任何解决方案.我想使其更像Google搜索,但使用ComboBox.

Is it possible to make Combobox editable while dropdown is opened? I haven't found any solution. I want to make it more like Google Search but using ComboBox.

推荐答案

问题:在编辑文本时显示 Combobox PopdownWindow

Question: Show Combobox PopdownWindow, while editing text

此示例将 ttk.Combobox 扩展到以下内容:

This example extends a ttk.Combobox to the following:

  • 键入时显示PopdownWindow
  • 在按键'< Down>'
  • 上打开PopdownWindow
  • 如果在 Listbox
  • 中的第一项上,则在按下键的'< Up'上关闭PopdownWindow
  • Show the PopdownWindow while typing
  • Open the PopdownWindow on Key-pressed '<Down>'
  • Close the PopdownWindow on Key-pressed '<Up', if at the first item in the Listbox

参考:

对于每个小部件,您都可以将Python函数和方法绑定到事件.

For each widget, you can bind Python functions and methods to events.

  • ttk :: combobox —带有弹出式选择列表的文本字段

  • ttk::combobox — text field with popdown selection list

    ttk :: combobox 在内部使用 entry listbox 小部件.

    1. 继承自 ttk.Combox

    import tkinter as tk
    import tkinter.ttk as ttk
    
    
    class Combobox(ttk.Combobox):
    

  • Helper函数,用于将内部 Toplevel Listbox 映射到 tkinter 对象.

    警告:这使用了 Tk/Tcl 内部,如有更改,恕不另行通知.
    这可能仅适用于经过测试的Tk/Tcl版本

    WARNING: This uses Tk/Tcl internals, which could change without notice.
    This may working only with the tested Tk/Tcl version!

    def _tk(self, cls, parent):
        obj = cls(parent)
        obj.destroy()
        if cls is tk.Toplevel:
            obj._w = self.tk.call('ttk::combobox::PopdownWindow', self)
        else:
            obj._w = '{}.{}'.format(parent._w, 'f.l')
        return obj
    

  • 初始化对象,获取内部引用并绑定到按键事件

  • Initalize the object, get the internal references and bind to Key-press events

    def __init__(self, parent, **kwargs):
        super().__init__(parent, **kwargs)
        self.popdown = self._tk(tk.Toplevel, parent)
        self.listbox = self._tk(tk.Listbox, self.popdown)
    
        self.bind("<KeyPress>", self.on_keypress, '+')
        self.listbox.bind("<Up>", self.on_keypress)
    

  • 按键处理程序,以显示或隐藏PopdownWindow并设置键盘焦点.

  • Key-pressed handler to show or hide the PopdownWindow and set the Keyboard focus.

    def on_keypress(self, event):
        if event.widget == self:
            state = self.popdown.state()
    
            if state == 'withdrawn' \
                    and event.keysym not in ['BackSpace', 'Up']:
                self.event_generate('<Button-1>')
                self.after(0, self.focus_set)
    
            if event.keysym == 'Down':
                self.after(0, self.listbox.focus_set)
    
        else:  # self.listbox
            curselection = self.listbox.curselection()
    
            if event.keysym == 'Up' and curselection[0] == 0:
                self.popdown.withdraw()
    
    

    用法:

    class App(tk.Tk):
        def __init__(self):
            super().__init__()
    
            values = ('one', 'two', 'three', 'four', 'five', 'six', 'seven')
    
            self.cb = Combobox(self, value=values)
            self.cb.grid(row=0, column=0)
    
    
    if __name__ == "__main__":
        App().mainloop()
    
    

    使用Python测试:3.5-'TclVersion':8.6'TkVersion':8.6

    Tested with Python: 3.5 - 'TclVersion': 8.6 'TkVersion': 8.6

    这篇关于使用tkinter编辑文本时显示下拉列表组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆