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?
I'd also like to set gridlines
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
几个月前我也遇到了同样的问题。
从 tk 文档 中:
基本上,您将一个标签应用于所有奇数行,另一个标签应用于每个偶数行,然后配置标签。
当您在树视图中创建项目时,向它们添加标签:
此代码在
tree
中创建一个元素,并且tags
参数将标签“oddrow”分配给该元素。使用“奇数行”和“偶数行”标签创建所有元素后,您可以为标签着色:
I had this same issue a few months ago.
From the tk docs:
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:
This code creates an element in
tree
, and thetags
argument assigns the tag 'oddrow' to the element.Once you've created all your elements with 'oddrow' and 'evenrow' tags, you can color the tags:
我意识到这是一个老问题,但仅用于在创建树后配置标签的记录(即尚未添加任何项目时)也可以。当项目稍后插入时,它们将被赋予适合其“奇数行”或“偶数行”标签的背景颜色。
I realise this is an old question but just for the record configuring the tags just after creating the tree (i.e. when no items have yet been added to it) also works. As items get inserted later on they will be given the background colour appropriate to their 'oddrow' or 'evenrow' tag.
这是无需任何额外模块即可运行的...可能会弄乱代码以使其与您自己的代码一起使用。
this is runnable without any extra modules... could mess with the code to make it work with your own.
这是为了创建一个 SQL 数据库。然后将数据库中的少数客户加载到列表框中。
然后您可以单击“测试新颜色”按钮以显示奇数行的颜色变化。只要您将 sqlalchemy 作为模块安装,就可以运行。
This is to create a SQL db. then loads the few customers from db into listbox.
then you can click test new colors button to show it changing colors for odd rows. this is runnable as long as you have sqlalchemy installed as module.
工作代码:python3.6
working code: python3.6
我制作了一个应用交替行颜色的函数。对于那些重新排列行并需要再次正确重新着色的人来说,这是一个更好的方法。
I made a function that applies alternating row color. This is a better way for those who are resorting the rows and need to recolor correctly again.
我对其进行了更好的更新,这适用于更高版本的 python 3.9+,也许更旧。
这将进行排序,您只需在函数中添加对它的调用即可更新您的树视图以及底部的排序函数,并且它将在排序后保留它。
I updated it a little bit better, and this is working with later versions of python 3.9+, maybe older.
This will sort and you can just ad the call to it in your function to update your treeview as well as your sort function at the base and it will keep it after sorting.