使用 tkinter 编辑文本时显示下拉组合框
Show combobox drop down while editing text using tkinter
是否可以在下拉菜单打开时使组合框可编辑?
我还没有找到任何解决方案。
我想让它更像 Google 搜索,但使用组合框。
Question: Show Combobox
PopdownWindow, while editing text
此示例将 ttk.Combobox
扩展为以下内容:
- 键入时显示 PopdownWindow
- 在按键时打开 PopdownWindow
'<Down>'
- 如果在
Listbox
中的第一项,则在按键 '<Up'
时关闭 PopdownWindow
参考:
-
For each widget, you can bind Python functions and methods to events.
ttk::combobox
— text field with popdown selection list
The ttk::combobox
uses the entry
and listbox
widgets internally.
继承自ttk.Combox
import tkinter as tk
import tkinter.ttk as ttk
class Combobox(ttk.Combobox):
Helper 函数,将内部 Toplevel
和 Listbox
映射到 tkinter
对象。
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
初始化对象,获取内部引用并绑定到按键事件
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 并设置键盘焦点的按键处理程序。
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()
测试 Python:3.5 - 'TclVersion':8.6 'TkVersion':8.6
是否可以在下拉菜单打开时使组合框可编辑? 我还没有找到任何解决方案。 我想让它更像 Google 搜索,但使用组合框。
Question: Show
Combobox
PopdownWindow, while editing text
此示例将 ttk.Combobox
扩展为以下内容:
- 键入时显示 PopdownWindow
- 在按键时打开 PopdownWindow
'<Down>'
- 如果在
Listbox
中的第一项,则在按键
'<Up'
时关闭 PopdownWindow
参考:
-
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.
继承自
ttk.Combox
import tkinter as tk import tkinter.ttk as ttk class Combobox(ttk.Combobox):
Helper 函数,将内部
Toplevel
和Listbox
映射到tkinter
对象。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
初始化对象,获取内部引用并绑定到按键事件
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 并设置键盘焦点的按键处理程序。
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()
测试 Python:3.5 - 'TclVersion':8.6 'TkVersion':8.6