gpt4 book ai didi

python - 如何在 tkinter 列表框中突出显示选择?

转载 作者:IT老高 更新时间:2023-10-28 21:11:11 28 4
gpt4 key购买 nike

我在单选模式下设置了 2 个单独的列表框。当我从 listboxA 中选择一个项目时,它会被突出显示,但是当我从 listboxB 中选择一个项目时,它会被突出显示,并且来自 listboxA 的项目> 保持事件状态,但未突出显示。如何让两者都突出显示?

最佳答案

简答:将每个列表框的 exportselection 属性设置为 False

Tkinter 起源于 X 窗口系统。 X 有一个概念叫做“选择”,它类似于系统剪贴板(更准确地说,剪贴板是“PRIMARY”选择)。默认情况下,一些 tkinter 小部件将它们的选择导出为 PRIMARY 选择。一个应用程序一次只能有一个 PRIMARY 选择,这就是当您在两个列表框之间单击时突出显示消失的原因。

Tkinter 使用列表框(以及文本和条目小部件)的 exportselection 配置选项让您控制此行为。将其设置为 False 可防止将选择导出到 X 选择,从而允许小部件在不同的小部件获得焦点时保留其选择。

例如:

the_listbox = tk.Listbox(..., exportselection=False)

引自 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.

关于python - 如何在 tkinter 列表框中突出显示选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10048609/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com