Sto tentando di utilizzare ttk.Trekiew per creare una tabella ordinabile (come da Does tkinter have a table widget? e https://www.daniweb.com/software-development/python/threads/350266/creating-table-in-python).Come modificare lo stile di un'intestazione in Treeview (Python ttk)
È facile farlo funzionare, ma ho qualche problema con lo stile. Lo stile predefinito per l'intestazione Treeview è il testo nero su uno sfondo bianco, che va bene. Tuttavia, nel mio codice sto usando:
ttk.Style().configure(".", font=('Helvetica', 8), foreground="white")
per formattare la GUI. Questo stile generale influisce anche sull'intestazione del widget Treeview. Poiché lo sfondo di intestazione di default è bianco, non riesco a vedere il testo (a meno che non tocchi il mouse sull'intestazione, che diventa blu chiaro).
Normalmente, sostituisco lo stile di un widget utilizzando un tag per modificare lo sfondo o il primo piano, ma non riesco a capire come regolare le intestazioni di Treeview. ttk.Treeview (...) non accetta alcun tag e ttk.Style(). configure ("Treeview", ...) non ha alcun effetto. Solo gli oggetti Treeview sembrano accettare tag quando si utilizza widget.insert (...).
Questo mi sconcerta, perché il ttk.Style generale(). Configurare ("", ...) fa influenzano le intestazioni TreeView, quindi dovrebbe essere possibile applicare un tag a loro.
Qualcuno sa come modificare lo stile di una voce Treeview?
Di seguito è riportato un esempio di lavoro minimo. Nota che il tag funziona per gli articoli ma non per le intestazioni, che lo stile Treeview non ha alcun effetto e che "." lo stile ha un effetto. Sto usando Python 2.7 su Windows 7 nel caso in cui ciò faccia la differenza.
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()
Sei un eroe! Grazie :) –