You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Python/Tk Treeview表格的背景全部设置为深色?

解决Tkinter Treeview深色主题下白色边框区域问题

问题根源在于ttk Treeview的默认fieldbackground属性为白色,该属性控制着表格内容区域外的背景色。只需在自定义样式中添加该属性并设置为深色,即可消除白色区域。

修改步骤

  1. 配置Dark.Treeview样式时,添加fieldbackground="#333333",与窗口背景色保持一致。
  2. 可选操作:若系统默认主题(如Windows的vista主题)不支持自定义fieldbackground,可先切换到支持自定义的clam主题。

修改后的关键代码片段

# 定义列样式
style = ttk.Style()
# 切换到clam主题以确保自定义样式生效
style.theme_use("clam")
style.configure("Dark.Treeview.Heading", background="#666666", foreground="#CCCCCC", font=('Helvetica', 10, 'bold'))
# 添加fieldbackground属性,消除白色区域
style.configure("Dark.Treeview", background="#333333", foreground="#CCCCCC", font=('Helvetica', 10), fieldbackground="#333333")
style.configure("Blue.Treeview.Cell", foreground="#6FA1D3")

完整修改代码

#!/usr/bin/python3

import os
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox

def get_files_and_sizes():
    files = os.listdir('.')
    files_and_sizes = {}
    for file in files:
        if os.path.isfile(file):
            size = os.path.getsize(file)
            files_and_sizes[file] = size
    return files_and_sizes

def display_details(file_name):
    details_window = tk.Toplevel()
    details_window.title("File Details")
    details_window.configure(background="#333333")

    label = ttk.Label(details_window, text="File Name: " + file_name, background="#333333", foreground="#CCCCCC")
    label.pack(padx=10, pady=10)

    close_button = ttk.Button(details_window, text="Close", command=details_window.destroy, style="Dark.TButton")
    close_button.pack(pady=5)

def display_files_and_sizes():
    def refresh():
        # 清空表格内容
        for row in tree.get_children():
            tree.delete(row)
        # 刷新文件列表和大小
        files_and_sizes = get_files_and_sizes()
        index = 1
        for file, size in files_and_sizes.items():
            tree.insert("", index, text=str(index), values=(file, size))
            index += 1

    def close():
        root.destroy()

    def show_details():
        item = tree.selection()[0]
        file_name = tree.item(item, "values")[0]
        display_details(file_name)

    def sort_column(col, reverse):
        data = [(tree.set(child, col), child) for child in tree.get_children('')]
        data.sort(reverse=reverse)

        for index, (val, child) in enumerate(data):
            tree.move(child, '', index)

        tree.heading(col, command=lambda: sort_column(col, not reverse))

    files_and_sizes = get_files_and_sizes()
    root = tk.Tk()
    root.title("Files and Sizes")
    root.configure(background="#333333")
    root.geometry("1024x768")

    # 定义列样式
    style = ttk.Style()
    # 切换到clam主题以支持自定义fieldbackground
    style.theme_use("clam")
    style.configure("Dark.Treeview.Heading", background="#666666", foreground="#CCCCCC", font=('Helvetica', 10, 'bold'))
    # 添加fieldbackground属性,消除白色区域
    style.configure("Dark.Treeview", background="#333333", foreground="#CCCCCC", font=('Helvetica', 10), fieldbackground="#333333")
    style.configure("Blue.Treeview.Cell", foreground="#6FA1D3")

    # 定义按钮样式
    style.configure("Dark.TButton", background="#444444", foreground="#CCCCCC")

    tree = ttk.Treeview(root, columns=("File", "Size"), style="Dark.Treeview")
    tree.heading("#0", text="Index", anchor=tk.W, command=lambda: sort_column("#0", False))
    tree.heading("File", text="File", anchor=tk.W, command=lambda: sort_column("File", False))
    tree.heading("Size", text="Size (bytes)", anchor=tk.W, command=lambda: sort_column("Size", False))
    tree["show"] = "headings"  # 隐藏默认的空列

    index = 1
    for file, size in files_and_sizes.items():
        tree.insert("", index, text=str(index), values=(file, size))
        index += 1

    # 设置第一列单元格样式
    tree.tag_configure("blue", foreground="#6FA1D3")

    for i in range(1, len(files_and_sizes) + 1):
        tree.tag_configure(str(i), foreground="#6FA1D3")

    tree.pack(expand=True, fill=tk.BOTH)

    refresh_button = ttk.Button(root, text="Refresh", command=refresh, style="Dark.TButton")
    refresh_button.pack(side=tk.LEFT, padx=5, pady=5)

    close_button = ttk.Button(root, text="Close", command=close, style="Dark.TButton")
    close_button.pack(side=tk.RIGHT, padx=5, pady=5)

    # 右键菜单
    menu = tk.Menu(root, tearoff=0, bg="#555555", fg="#CCCCCC")
    menu.add_command(label="Details", command=show_details)

    def popup(event):
        if tree.identify_region(event.x, event.y) == "cell":
            menu.post(event.x_root, event.y_root)

    tree.bind("<Button-3>", popup)

    root.mainloop()

if __name__ == "__main__":
    display_files_and_sizes()

补充说明

  • fieldbackground是ttk Treeview的专属属性,控制表格内容区域外的背景色,默认值为白色,这就是你看到的白色区域的来源。
  • 部分系统默认主题不支持修改该属性,切换到clam主题可以确保自定义样式正常生效。

内容的提问来源于stack exchange,提问作者Benj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 00:08:14