tkinter change style问题

2024-04-26 07:27:20 发布

您现在位置:Python中文网/ 问答频道 /正文

现在我有这个代码:

tree = ttk.Treeview(root)
style = ttk.Style()
style.configure("Treeview", foreground='#00337f',background="#ffc61e")
style.configure("Treeview.Heading", background="purple",foreground='#00337f',font=(20))
tree["columns"] = ("one", "two", "three")
tree.column("one", width=150)
tree.column("two", width=150)
tree.column("three", width=210)
tree.heading("one", text="Naar")
tree.heading("two", text="Spoor")
tree.heading("three", text="Vetrektijd")
tree.tag_configure('monospace', font=20)
tree['show'] = 'headings'
tree.place(x=0, y=0)

除了三件事外,一切正常:

  • 每当我运行这个程序时,treeview的背景仍然是白色的,但是从一开始就需要是黄色的。现在只要树视图被填满,它就会变成黄色。

  • 第二个问题是,我把标题的背景改为紫色,你可以在上面的代码中看到,但它仍然是白色的。我怎么才能把它变成紫色呢?

  • 第三个问题是树视图填充后仍有一行:

enter image description here


Tags: 代码texttreestyleconfigurecolumnwidthone
1条回答
网友
1楼 · 发布于 2024-04-26 07:27:20

第一个问题可以通过在Treeview样式的配置中添加fieldbackground="#ffc61e"来解决。“背景”选项仅对应于项目的背景。然而,并不是所有的主题都考虑fieldbackground选项(参见第二个问题)。在

第二个问题是主题相关。您使用的主题不支持标题的颜色更改,因此我建议您使用另一个主题,如“clam”或“alt”:style.theme_use('clam')。在

要删除标题中按钮的边框,可以将边框宽度设置为0: style.configure("Treeview.Heading", borderwidth=0)。在

不幸的是,树形边框的颜色不能设置为所有颜色:

style.configure("Treeview", lightcolor="#ffc61e", bordercolor="#ffc61e",
                darkcolor="#ffc61e")

相关问题 更多 >