简体   繁体   English

ttk 树视图:选定的颜色

[英]ttk treeview: selected color

The selected row of my ttk treeview shows as a dark blue background with the text white.我的 ttk 树视图的选定行显示为深蓝色背景,文本为白色。

If I set the color of the row with a tag, for example:如果我用标签设置行的颜色,例如:

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

and configure the tag as a color, for example:并将标签配置为颜色,例如:

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

and select the oddrow, the background color does not change (it remains lightgrey) while the text changes from black to white.并选择奇数行,背景颜色不变(保持浅灰色),而文本从黑色变为白色。 How can I get the selected row background to be dark blue, whether or not the row is tagged with a color?如何使所选行的背景变为深蓝色,无论该行是否带有颜色标记?

Rows not tagged display as black on white, or when selected as white on dark blue.行无标记为黑色显示为黑色,或在深蓝色上选择白色时。

I tried我试过了

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

but that didn't do anything.但这没有做任何事情。

EDIT: I suppose that when I select an item I could re-tag it as not oddrow, then go back when it's un-selected, but that is rather inelegant.编辑:我想当我选择一个项目时,我可以将它重新标记为 notoddrow,然后在它未被选中时返回,但这相当不雅。

From the TkDocs tutorial for trees, it seems you can:从树的TkDocs 教程中,您似乎可以:

  • create a tag with the desired colors (for a selected row)创建具有所需颜色的标签(针对选定行)

Then, catch the virtual events from the treeview:然后,从树视图中捕获虚拟事件:

  • assign the tag to a row when it gets the focus获得焦点时将标签分配给一行
  • unassign the tag from the row when it loses focus失去焦点时从行中取消分配标签

Here's the specific paragraph in the documentation I used:这是我使用的文档中的特定段落:

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

Along with some code from the tutorial:连同教程中的一些代码:

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

note: I'm not sure this will work.注意:我不确定这会起作用。 I'll have to dig up the code to see what I did.我必须挖掘代码才能看到我做了什么。

If anyone looking for an answer to change selected color for tkinter treeview, you can check below code.如果有人正在寻找更改 tkinter 树视图所选颜色的答案,您可以查看以下代码。

You have change the state "selected" rather than "active".您已更改状态“选定”而不是“活动”。

style = ttk.Style()
style.configure("Treeview",
                background="#E1E1E1",
                foreground="#000000",
                rowheight=25,
                fieldbackground="#E1E1E1")
style.map('Treeview', background=[('selected', '#BFBFBF')])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM