How to edit title style in Treeview (Python ttk)

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

Working with him is easy, but I am having problems with the style. The default style for the Treeview title is black text on a white background, and that's fine. However, in my code I use:

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

to format my GUI. This comprehensive style also affects the title of the Treeview widget. Since the title bar is white by default, I don’t see the text (unless I find the mouse pointer over the title that turns it blue).

I usually redefine the style of the widget using a tag to change the background or foreground, but I can’t understand for life how to customize Treeview headers! ttk.Treeview (...) does not accept any tags, but ttk.Style (). configure ("Treeview", ...) has no effect. It seems that only Treeview elements accept tags when using widget.insert (...).

This is confusing to me because comprehensive ttk.Style (). configure (".", ...) affects Treeview headers, so you can apply a tag to them.

Does anyone know how to change the title style of a Treeview?

Below is a minimal working example. Note that the tag works for elements, but not for headers, that the Treeview style has no effect and that "." style has an effect. I am using Python 2.7 for Windows 7 if that matters.

  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() 
+6
source share
2 answers

it 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

+6
source

You can change the font used in Treeview headers using the default font "TkHeadingFont".

For instance:

 font.nametofont('TkHeadingFont').configure(size = 15) 
+1
source

All Articles