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

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

问题描述

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

解决方案

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

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

  • 键入时显示 PopdownWindow
  • ''
  • 打开PopdownWindow
  • 如果在 Listbox
  • 中的第一项,则在按下按键 ' 时关闭 PopdownWindow
<小时>

参考:

<小时>

  1. 继承自ttk.Combox

    将 tkinter 导入为 tk将 tkinter.ttk 导入为 ttk类组合框(ttk.Combobox):

  2. Helper 函数,将内部的 ToplevelListbox 映射到 tkinter 对象.

    <块引用>

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

    def _tk(self, cls, parent):obj = cls(父)obj.destroy()如果 cls 是 tk.Toplevel:obj._w = self.tk.call('ttk::combobox::PopdownWindow', self)别的:obj._w = '{}.{}'.format(parent._w, 'f.l')返回对象

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

    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("", self.on_keypress, '+')self.listbox.bind("", self.on_keypress)

  4. 用于显示或隐藏 PopdownWindow 并设置键盘焦点的按键处理程序.

    def on_keypress(self, event):如果 event.widget == 自我:状态 = self.popdown.state()如果状态 == '撤回' 和 event.keysym 不在 ['BackSpace', 'Up'] 中:self.event_generate('')self.after(0, self.focus_set)如果 event.keysym == 'Down':self.after(0, self.listbox.focus_set)其他:# self.listboxcurselection = self.listbox.curselection()如果 event.keysym == 'Up' 和 curselection[0] == 0:self.popdown.withdraw()

    <块引用>

    用法:

    类应用程序(tk.Tk):def __init__(self):super().__init__()值 = ('一', '二', '三', '四', '五', '六', '七')self.cb = Combobox(self, value=values)self.cb.grid(row=0, column=0)如果 __name__ == "__main__":应用程序().主循环()

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

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.

解决方案

Question: Show Combobox PopdownWindow, while editing text

This example extends a ttk.Combobox to the following:

  • 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

Reference:


  1. Inherit from ttk.Combox

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

  2. Helper function, to map the internal Toplevel and Listbox to a tkinter object.

    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
    

  3. 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)
    

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

    Usage:

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

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

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

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