问题
I have this little Tkinter GUI where I have a table. I want to make the text bold in the headings and also change its background color. I know that we can do this by ttk.Style() and configure but nothing is changing in the table. It's still looking plain or am I doing this wrong.
Please help.
from tkinter import ttk
import tkinter as tk
from tkinter import *
window = tk.Tk()
window.state('zoomed')
treev = ttk.Treeview(window, selectmode ='browse')
treev.place(x= 600, y= 200, width= 350, height=350)
treev["columns"] = ('1', '2')
treev['show'] = 'headings'
style = ttk.Style()
style.configure('mytreeview.Headings', background='gray', font=('Arial Bold', 10))
ID = [1,2,3,4,5]
Names = ['Tom', 'Rob', 'Tim', 'Jim', 'Kim']
treev.column("1", width = 100, anchor ='c')
treev.column("2", width = 100, anchor ='c')
treev.heading("1", text ="ID")
treev.heading("2", text ="Names")
for x, y in zip(ID, Names):
treev.insert("", 'end', values =(x, y))
window.mainloop()
回答1:
To configure an style you need a layout that is called so:
style.layout('my.Treeview',
[('Treeview.field', {'sticky': 'nswe', 'border': '1', 'children': [
('Treeview.padding', {'sticky': 'nswe', 'children': [
('Treeview.treearea', {'sticky': 'nswe'})
]})
]})
])
This code makes a new layout called my.Treeview and copies the data of Treeview. Then, after you have a layout created with that name you can configure it with:
style.configure('my.Treeview.Heading', background='gray', font=('Arial Bold', 10))
and dont forget to use that style on the widget you like with:
treev = ttk.Treeview(window, selectmode ='browse',style='my.Treeview')
来源:https://stackoverflow.com/questions/65536845/add-styles-to-headings-text-bold-and-backgroundcolor-of-a-treeview-table-tki