如何在 tkinter 列表框中突出显示选择? [英] How to keep selections highlighted in a tkinter Listbox?

查看:42
本文介绍了如何在 tkinter 列表框中突出显示选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单选模式下设置了 2 个单独的列表框.当我从 listboxA 中选择一个项目时,它会突出显示,但是当我从 listboxB 中选择一个项目时,它会突出显示,而 listboxA 中的项目> 保持活动状态,但不突出显示.如何让两者都突出显示?

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?

推荐答案

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

Short answer: set the exportselection attribute of each listbox to False

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

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

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.

例如:

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

引自 官方 tk 文档:

导出选择指定小部件中的选择是否也应该是X 选择.该值可能具有以下接受的任何形式Tcl_GetBoolean,例如 true、false、0、1、yes 或 no.如果选择被导出,然后在小部件中选择取消选择当前 X 选择,在小部件外选择会取消选择任何小部件选择,小部件将响应选择检索请求当它有选择时.默认通常是用于要导出的小部件选择.

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.

这篇关于如何在 tkinter 列表框中突出显示选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆