Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

python - How to keep selections highlighted in a tkinter Listbox?

I have 2 separated List-boxes set on single selection mode. When I select an item from listboxA, it gets highlighted, but when I select an item from listboxB, it gets highlighted, and the item from listboxA remains active, but isn't highlighted. How can I keep both highlighted?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...