pythonpython-3.xtkinter

tkinter.Listbox has a "shadow selection" beside the proper selection. How to sync both?


I built and populated a tkinter.Listbox. Now I have events that will select the item at index index. Like so:

listbox.selection_clear(0, tk.END)
listbox.select_set(index)

And it works in that the entry with index index is in fact selected.

However, when using 'tab' keys to move to other widgets that also have the power to select items in that listbox, and then returning to the listbox, there is a shadow selection, that appears not to be the anchor (at least, listbox.selection_anchor(index) did not solve this issue for me) on the selection that was active, when I last left focus on the listbox. Using 'up' and 'down' keys will take control of the active selection. However, they will start not at the proper selection (010_adf in below example), but on that shadow (007_adf) that I can only specify closer by providing this screenshot:

enter image description here

Fig: The "shadow" in question is around entry 007_adf. The proper selection is 010.adf. How to sync the shadow to the proper selection?


Solution

  • That "shadow" designates the active item in the listbox. Think of it like a cursor in a text widget. You can set it with the activate method.

    If you want to hide it altogether you can set activestyle='none'. Or, you can set it when you set the selection:

    ...
    lb = tk.Listbox(...)
    ...
    lb.selection_set(4,6)
    lb.activate(4)
    ...