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
+1
A:
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.
edit: 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.OnEntryDown)
def OnEntryDown(self, event):
self.listbox.yview_scroll(1,"units")
def OnEntryUP(self, event):
self.listbox.yview_scroll(-1,"units")
Bryan Oakley
2010-08-24 19:49:44
That makes sense. But how do I control them explicitly? So far I've just used mylistbox.config(yscrollcommand=myscrollbar.set) and myscrollbar.config(command=mylistbox.yview).
Jeff
2010-08-25 02:20:35
@Jeff: I've added some example code to my answer to address your question.
Bryan Oakley
2010-08-25 10:59:04
thanks! various websites made it sound complex, but with your example it's super easy.
Jeff
2010-08-28 18:30:04