如何滚动不活动的 Tkinter ListBox? [英] How to scroll an inactive Tkinter ListBox?
问题描述
我正在用 Python 编写 Tkinter GUI.它有一个用于搜索的条目,其下方有一个结果列表框.ListBox 也有一个滚动条.如何在不将焦点从搜索字段移开的情况下使用鼠标和箭头键滚动以在 ListBox 中工作?IE 我希望用户能够键入搜索、滚动并继续键入,而不必在小部件之间来回切换.谢谢
I'm writing a Tkinter GUI in Python. It has an Entry for searching with a results ListBox below it. The ListBox also has a Scrollbar. How can I get scrolling with the mouse and arrow keys to work in the ListBox without switching focus away from the search field? IE I want the user to be able to type a search, scroll around, and keep typing without having to tab back and forth between widgets. Thanks
推荐答案
将绑定添加到入口小部件,当用户调用列表框 yview
和/或 see
命令时上下按或使用上/下滚轮.
Add bindings to the entry widget that call the listbox yview
and/or see
commands when the user presses up and down or uses the up/down scrollwheel.
例如,您可以对箭头键执行以下操作:
For example, you can do something like this for the arrow keys:
class App(Tkinter.Tk):
def __init__(self):
Tkinter.Tk.__init__(self)
self.entry = Tkinter.Entry()
self.listbox = Tkinter.Listbox()
self.entry.pack(side="top", fill="x")
self.listbox.pack(side="top", fill="both", expand=True)
for i in range(100):
self.listbox.insert("end", "item %s" % i)
self.entry.bind("<Down>", self.OnEntryDown)
self.entry.bind("<Up>", self.OnEntryUp)
def OnEntryDown(self, event):
self.listbox.yview_scroll(1,"units")
def OnEntryUp(self, event):
self.listbox.yview_scroll(-1,"units")
这篇关于如何滚动不活动的 Tkinter ListBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!