使用 tkinter 编辑文本时显示组合框下拉菜单 [英] Show combobox drop down while editing text using tkinter
问题描述
是否可以在打开下拉菜单时使 Combobox 可编辑?我还没有找到任何解决方案.我想让它更像 Google 搜索,但使用 ComboBox.
问题:在编辑文本时显示
Combobox
PopdownWindow
此示例将 ttk.Combobox
扩展为以下内容:
- 键入时显示 PopdownWindow
- 按
'
' 打开PopdownWindow - 如果在
Listbox
中的第一项,则在按下按键
' 时关闭 PopdownWindow
参考:
- <块引用>
对于每个小部件,您可以将 Python 函数和方法绑定到事件.
- <块引用>
ttk::combobox
在内部使用entry
和listbox
小部件.
继承自
ttk.Combox
将 tkinter 导入为 tk将 tkinter.ttk 导入为 ttk类组合框(ttk.Combobox):
Helper 函数,将内部的
<块引用>Toplevel
和Listbox
映射到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')返回对象
初始化对象,获取内部引用并绑定到按键事件
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) 用于显示或隐藏 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 theListbox
Reference:
For each widget, you can bind Python functions and methods to events.
ttk::combobox
— text field with popdown selection listThe
ttk::combobox
uses theentry
andlistbox
widgets internally.
Inherit from
ttk.Combox
import tkinter as tk import tkinter.ttk as ttk class Combobox(ttk.Combobox):
Helper function, to map the internal
Toplevel
andListbox
to atkinter
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
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)
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屋!