ttk 树视图:交替行颜色 [英] ttk treeview: alternate row colors

查看:89
本文介绍了ttk 树视图:交替行颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为 treeview 小部件设置样式,以便交替行具有不同的背景颜色,例如,第 1、3、5 行具有白色背景,第 2、4、6 行具有浅蓝色-灰色背景?

How can I set a style for treeview widgets so that alternate rows have different background colors, for example, rows 1,3,5 have white backgrounds and rows 2,4,6 have light blue-grey backgrounds?

我还想设置gridlines.

推荐答案

几个月前我遇到了同样的问题.

I had this same issue a few months ago.

来自 tk 文档:

You can assign a list of tags to each item using the "tags" 
item configuration option (again, when creating the item or later on).

Tag configuration options can then be specified, which will then 
apply to all items having that tag.

基本上,您将一个标签应用于所有奇数行,将不同的标签应用于每个偶数行,然后配置标签.

Basically, you apply one tag to all of the odd-numbered rows, a different tag to each of the even-numbered rows, and then configure the tags.

当你在树视图中创建项目时,给它们添加标签:

When you create the items inside the treeview, add tags to them:

tree.insert('', 'end', text = 'your text', tags = ('oddrow',))

此代码在 tree 中创建一个元素,tags 参数将标记 'oddrow' 分配给该元素.

This code creates an element in tree, and the tags argument assigns the tag 'oddrow' to the element.

使用oddrow"和evenrow"标签创建所有元素后,您可以为标签着色:

Once you've created all your elements with 'oddrow' and 'evenrow' tags, you can color the tags:

tree.tag_configure('oddrow', background='orange')
tree.tag_configure('evenrow', background='purple')

这篇关于ttk 树视图:交替行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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