如何在Treeview中设置表头文字颜色


import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
root.title(‘text’)
root.geometry(‘800x400’)
frame = tk.LabelFrame(root, text=’分析区’, font=(‘宋体’, 10), fg=’red’, labelanchor=’n’, bd=1, height=140,
relief=’groove’)
frame.pack(side=’left’, pady=2, padx=2)
fram1 = tk.Frame(frame, width=20, height=20)
fram1.pack(side=’left’)

mark = [str(i) for i in range(1, 21)]
table = ttk.Treeview(frame, columns=mark, height=5, style=’Treeview’, show=’headings’)
table.tag_configure(‘all’, foreground=’red’)

for j in range(len(mark)):
table.heading(column=mark[j], text=mark[j], anchor=’center’)
table.column(j+1, minwidth=0, width=28, anchor=’center’, stretch=tk.NO)

table.pack()

root.mainloop()

网上查了很多实在是没有看到有关表头颜色控制的资料

Jason990420
最佳答案

定制和创建主题和样式

  • 设置样式

    style = ttk.Style()
  • 设置所有部件的外观

    # 'winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative'
    style.theme_use("default")
  • 设置样式的新选项值

    style.configure(
      style_name + '.Heading',
      foreground=foreground,
      background=background,
      borderwidth=borderwidth,
    )
  • 设置样式元素的动态行

    # active - 鼠标是否正在部件上方
    # pressed - 部件是否正被点击
    # !focus - 非聚焦中
    style.map(
      style_name + '.Heading', 
      background=[('pressed', '!focus', background), ('active', foreground),],
      foreground=[('pressed', '!focus', foreground), ('active', background),],
    )

Demo Code

import tkinter as tk
from tkinter import font as tkfont
import tkinter.ttk as ttk


root = tk.Tk()
root.title('Table')

columns = [str(i+1) for i in range(10)]
table = ttk.Treeview(root, columns=columns, show='headings')
table.pack()

font = ('Courier New', 20)
# extra one space reserved for each column
width = tkfont.Font(font=font).measure('000')
for column in columns:
    table.heading(column=column, text=column)
    table.column(column, width=width, anchor='center', stretch=tk.NO)

# Set color of headings
white, blue, green = 'white', 'blue', 'green'
style = ttk.Style()

# Better with extra one space for each column when setting
# 'winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative'
style.theme_use('winnative')

style_name = 'Treeview'
style.configure(
    style_name + '.Heading',
    foreground=white,
    background=blue,
    borderwidth=1,
    font=font,
)
style.map(
    style_name + '.Heading', 
    background=[('pressed', '!focus', green), ('active', white),],
    foreground=[('pressed', '!focus', white), ('active', blue),],
)

root.mainloop()

file

3个月前 评论
讨论数量: 3

import tkinter as tk import tkinter.ttk as ttk

root = tk.Tk() root.title('text') root.geometry('800x400')

frame = tk.LabelFrame(root, text='分析区', font=('宋体', 10), fg='red', labelanchor='n', bd=1, height=140, relief='groove') frame.pack(side='left', pady=2, padx=2) fram1 = tk.Frame(frame, width=20, height=20) fram1.pack(side='left')

mark = [str(i) for i in range(1, 21)] table = ttk.Treeview(frame, columns=mark, height=5, style='Treeview', show='headings') table.tag_configure('all', foreground='red')

for j in range(len(mark)):

# 设置表头文字颜色为红色
heading_font = ('宋体', 10, 'bold')  # 可以根据需要调整字体样式
table.heading(column=mark[j], text=mark[j], anchor='center', font=heading_font)
table.column(j+1, minwidth=0, width=28, anchor='center', stretch=tk.NO)

table.pack()

root.mainloop()

3个月前 评论
LiTtianS (楼主) 3个月前
Jason990420

定制和创建主题和样式

  • 设置样式

    style = ttk.Style()
  • 设置所有部件的外观

    # 'winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative'
    style.theme_use("default")
  • 设置样式的新选项值

    style.configure(
      style_name + '.Heading',
      foreground=foreground,
      background=background,
      borderwidth=borderwidth,
    )
  • 设置样式元素的动态行

    # active - 鼠标是否正在部件上方
    # pressed - 部件是否正被点击
    # !focus - 非聚焦中
    style.map(
      style_name + '.Heading', 
      background=[('pressed', '!focus', background), ('active', foreground),],
      foreground=[('pressed', '!focus', foreground), ('active', background),],
    )

Demo Code

import tkinter as tk
from tkinter import font as tkfont
import tkinter.ttk as ttk


root = tk.Tk()
root.title('Table')

columns = [str(i+1) for i in range(10)]
table = ttk.Treeview(root, columns=columns, show='headings')
table.pack()

font = ('Courier New', 20)
# extra one space reserved for each column
width = tkfont.Font(font=font).measure('000')
for column in columns:
    table.heading(column=column, text=column)
    table.column(column, width=width, anchor='center', stretch=tk.NO)

# Set color of headings
white, blue, green = 'white', 'blue', 'green'
style = ttk.Style()

# Better with extra one space for each column when setting
# 'winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative'
style.theme_use('winnative')

style_name = 'Treeview'
style.configure(
    style_name + '.Heading',
    foreground=white,
    background=blue,
    borderwidth=1,
    font=font,
)
style.map(
    style_name + '.Heading', 
    background=[('pressed', '!focus', green), ('active', white),],
    foreground=[('pressed', '!focus', white), ('active', blue),],
)

root.mainloop()

file

3个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!