Tkinter listbox change highlighted item programmatically

Because listboxes allow for single vs. continuous vs. distinct selection, and also allow for an active element, this question is ambiguous. The docs explain all the different things you can do.

The selection_set method adds an item to the current selection. This may or may not unselect other items, depending on your selection mode.

If you want to guarantee that you always get just that one item selected no matter what, you can clear the selection with selection_clear(0, END), then selection_set that one item.

If you want to also make the selected item active, also call activate on the item after setting it.

To understand about different selection modes, and how active and selected interact, read the docs.

Tags:

Python

Tkinter