8

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

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

self.tree.item(item, tags=('oddrow'))

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

self.tree.tag_configure('oddrow', background='lightgrey')

并选择奇数行,背景颜色不会改变(它仍然是浅灰色),而文本从黑色变为白色。如何使选定的行背景为深蓝色,无论该行是否用颜色标记?

行未标记为白色的黑色,或在深蓝色上选择为白色时。

我试过了

ttk.Style().configure('Treeview', selectbackground='blue')

但这并没有做任何事情。

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

4

2 回答 2

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). 

连同教程中的一些代码:

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

注意:我不确定这是否可行。我将不得不挖掘代码以查看我做了什么。

于 2011-10-27T11:21:20.290 回答
1

如果有人正在寻找更改 tkinter 树视图的选定颜色的答案,您可以查看以下代码。

您已将状态更改为“已选择”而不是“活动”。

style = ttk.Style()
style.configure("Treeview",
                background="#E1E1E1",
                foreground="#000000",
                rowheight=25,
                fieldbackground="#E1E1E1")
style.map('Treeview', background=[('selected', '#BFBFBF')])
于 2020-08-13T18:53:22.540 回答