【问题标题】:tkinter change style questionstkinter 改变风格问题
【发布时间】:2017-10-26 14:34:54
【问题描述】:

现在我有这个代码:

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)

一切正常,除了 3 件事:

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

  • 第二个问题是我将标题的背景更改为紫色,如您在上面的代码中所见,但它仍然是白色的。那么我怎样才能让它变成紫色呢?

  • 第三个问题是treeview填满后还有一行:

【问题讨论】:

    标签: python tkinter treeview


    【解决方案1】:

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

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

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

    很遗憾,这对Treeview边框不起作用,所以解决方法是将边框的所有颜色设置为背景色:

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

    【讨论】:

    • 嗨。我尝试了你的 fieldbackground 东西,但它只变成黄色,我在表格内插入了一些东西。不是从一开始我就需要背景是黄色的,从一开始背景仍然是白色的
    • @Klaas 它不适用于每个主题,请先尝试更改主题。
    • 谢谢它现在可以工作了。第三个问题我的意思是:nl.tinypic.com/r/28ti3wz/9 它周围有一条白线。我也需要它是黄色的
    • 这是我的最后一个问题,希望你能帮助我?
    猜你喜欢
    • 2017-07-22
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    相关资源
    最近更新 更多