Josh
Josh

Reputation: 2399

How to scroll to a selected item in a ScrolledListBox in python?

I am developing an application that displays a large database of items.

There is also a search bar allowing the user to search that database rather than scrolling through each item to find the one they need. When the search returns True (a match was made) the information needed shows up in a textbox and more options in another listbox (not scrolled now), but the ScrolledListBox does not auto-scroll to the selected item even if I am using selection_set() or list_box.active().

I got it! I had to use the list_box.yview_scroll() command! That takes 2 arguments: 1) The integer/index of where to go 2) Line by line or by pages(UNITS or PAGES)

But I do find the .see method to be more efficient!

Upvotes: 3

Views: 4094

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386285

Tkinter doesn't have a ScrolledListBox class. Can you be more specific about which ScrolledListBox implementation you're using?

That being said, the scrollable widgets in Tkinter typically have a see method that will scroll enough to make sure the passed-in index is visible.

In the comment to this answer you say you are using pmw.ScrolledListBox. According to the documentation for the ScrolledListBox widget:

In addition, methods from the Tkinter.Listbox class are forwarded by this megawidget to the listbox component.

Therefore, you can call any method on that widget that you can call on a Listbox widget. As I mentioned earlier, the Listbox class has a see method, so you should be able to do something like:

the_widget.see(index)

In the above example index is the row in the listbox you want to see.

Upvotes: 5

Related Questions