如何在 Treeview (Python ttk) 中编辑标题的样式 [英] How to edit the style of a heading in Treeview (Python ttk)

查看:29
本文介绍了如何在 Treeview (Python ttk) 中编辑标题的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ttk.Treeview 制作可排序的表格(根据 是否tkinter 有表格小部件吗?https://www.daniweb.com/software-development/python/threads/350266/creating-table-in-python).

I am trying to use ttk.Treeview to make a sortable table (as per Does tkinter have a table widget? and https://www.daniweb.com/software-development/python/threads/350266/creating-table-in-python).

让它开始工作很容易,但我在样式方面遇到了一些问题.Treeview 标题的默认样式是白底黑字,这很好.但是,在我使用的代码中:

Getting it to work is easy, but I'm having some issues with the styling. The default style for the Treeview heading is black text on a white background, which is fine. However, in my code I'm using:

ttk.Style().configure(".", font=('Helvetica', 8), foreground="white")

格式化我的GUI.这种总体风格也会影响 Treeview 小部件的标题.因为默认标题背景是白色,所以我看不到文本(除非我将鼠标悬停在标题上,它会变成淡蓝色).

to format my GUI. This overarching style also affects the heading of the Treeview widget. Because the default heading background is white, I can not see the text (unless I mouse-over the heading, which turns it light-blue).

通常,我会使用标签覆盖小部件的样式来更改背景或前景,但我终生无法弄清楚如何调整 Treeview 标题!ttk.Treeview(...) 不接受任何标签,并且 ttk.Style().configure("Treeview", ...) 没有效果.使用 widget.insert(...) 时,只有 Treeview 项目似乎接受标签.

Normally, I'd override the style of a widget using a tag to change either the background or foreground, but I can't for the life of me figure out how to adjust the Treeview headers! ttk.Treeview(...) doesn't accept any tags, and ttk.Style().configure("Treeview", ...) has no effect. Only the Treeview items appear to accept tags when using widget.insert(...).

这让我感到困惑,因为总体 ttk.Style().configure(".",...) 确实 影响 Treeview 标题,因此应该可以对它们应用标签.

This baffles me, because the overarching ttk.Style().configure(".",...) does affect the Treeview headings, so it should be possible to apply a tag to them.

有谁知道如何改变 Treeview 标题的样式?

下面是一个最小的工作示例.请注意,该标记适用于项目但不适用于标题,Treeview 样式无效,并且."风格确实有影响.我在 Windows 7 上使用 Python 2.7 以防万一.

Below is a Minimum Working Example. Notice that the tag works for items but not for headings, that the Treeview style has no effect, and that the "." style does have an effect. I'm using Python 2.7 on Windows 7 in case that makes a difference.

    from Tkinter import *
    import ttk

    header = ['car', 'repair']
    data = [
    ('Hyundai', 'brakes') ,
    ('Honda', 'light') ,
    ('Lexus', 'battery') ,
    ('Benz', 'wiper') ,
    ('Ford', 'tire')]

    root = Tk()
    frame = ttk.Frame(root)
    frame.pack()
    table = ttk.Treeview(frame, columns=header, show="headings")
    table.pack()

    ## table.tag_configure('items', foreground='blue')
    ## ttk.Style().configure("Treeview", background='red', foreground='yellow')
    ## ttk.Style().configure(".", font=('Helvetica', 8), foreground="white")

    for col in header:
        table.heading(col, text=col.title(), command=lambda c=col: sortby(table, c, 0))
    for item in data:
        table.insert('', 'end', values=item, tags=('items',))

    def sortby(tree, col, descending):
        """sort tree contents when a column header is clicked on"""
        # grab values to sort
        data = [(tree.set(child, col), child) \
            for child in tree.get_children('')]
        # if the data to be sorted is numeric change to float
        #data =  change_numeric(data)
        # now sort the data in place
        data.sort(reverse=descending)
        for ix, item in enumerate(data):
            tree.move(item[1], '', ix)
        # switch the heading so it will sort in the opposite direction
        tree.heading(col, command=lambda col=col: sortby(tree, col, \
            int(not descending)))

    root.mainloop()

推荐答案

这在我所在的地方有效 -

this works where I am -

style = ttk.Style()
style.configure(".", font=('Helvetica', 8), foreground="white")
style.configure("Treeview", foreground='red')
style.configure("Treeview.Heading", foreground='green') #<----

http://www.tkdocs.com/tutorial/styles.html

这篇关于如何在 Treeview (Python ttk) 中编辑标题的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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