ttk 树视图:选定的颜色

foo*_*ion 6 python treeview tkinter ttk

我的 ttk 树视图的选定行显示为深蓝色背景,文本为白色。

如果我用标签设置行的颜色,例如:

self.tree.item(item, tags=('oddrow'))
Run Code Online (Sandbox Code Playgroud)

并将标签配置为颜色,例如:

self.tree.tag_configure('oddrow', background='lightgrey')
Run Code Online (Sandbox Code Playgroud)

并选择奇数行,背景颜色不变(保持浅灰色),而文本从黑色变为白色。如何使所选行的背景变为深蓝色,无论该行是否带有颜色标记?

行无标记为黑色显示为黑色,或在深蓝色上选择白色时。

我试过

ttk.Style().configure('Treeview', selectbackground='blue')
Run Code Online (Sandbox Code Playgroud)

但这没有做任何事情。

编辑:我想当我选择一个项目时,我可以将它重新标记为 notoddrow,然后在它未被选中时返回,但这相当不雅。

Mat*_*ick 5

TkDocs树教程中,您似乎可以:

  • 创建具有所需颜色的标签(针对选定的行)

然后,从树视图中捕获虚拟事件:

  • 当获得焦点时将标签分配给一行
  • 当行失去焦点时取消分配该标签

这是我使用的文档中的具体段落:

The treeview will generate virtual events "<TreeviewSelect>", "<TreeviewOpen>" 
and "<TreeviewClose>" which allow you to monitor changes to the widget made 
by the user.   You can use the "selection" method to determine the current 
selection (the selection can also be changed from your program). 
Run Code Online (Sandbox Code Playgroud)

以及教程中的一些代码:

tree.tag_configure('ttk', background='yellow')
tree.tag_bind('ttk', '<1>', itemClicked); # the item clicked can be found via tree.focus()
Run Code Online (Sandbox Code Playgroud)

注意:我不确定这是否有效。我必须挖掘代码才能看看我做了什么。