Short answer: set the exportselection
attribute of each listbox to False
Tkinter has its roots in the X windowing system. X has a concept called a "selection", which is similar to the system clipboard (more accurately, the clipboard is the "PRIMARY" selection). By default, several of the tkinter widgets export their selection to be the PRIMARY selection. An application can only have one PRIMARY selection at a time, which is why the highlight disappears when you click between two listboxes.
Tkinter gives you control over this behavior with the exportselection
configuration option for the listbox (and text and entry widgets). Setting it to False
prevents the export of the selection to the X selection, allowing the widget to retain its selection when a different widget gets focus.
For example:
the_listbox = tk.Listbox(..., exportselection=False)
Quoting from the official tk documentation:
exportselection
Specifies whether or not a selection in the widget should also be the
X selection. The value may have any of the forms accepted by
Tcl_GetBoolean, such as true, false, 0, 1, yes, or no. If the
selection is exported, then selecting in the widget deselects the
current X selection, selecting outside the widget deselects any widget
selection, and the widget will respond to selection retrieval requests
when it has a selection. The default is usually for widgets to export
selections.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…