简体   繁体   中英

ttk.Treeview remove row focus color

I would like to remove the focus color in ttk.Treeview while keeping the table rows clickable and keep there assigned background colors. Here is my MWE with a Treeview as a table:

try:
    import Tkinter as Tk
    from Tkinter import ttk
except ModuleNotFoundError:
    import tkinter as Tk
    from tkinter import ttk

if __name__ == '__main__':
    root = Tk.Tk()
    frame = Tk.Frame(root)

    tree = ttk.Treeview(frame.master, columns=("Col1", "Col2", "Col3"), show="headings")
    tree.heading('Col1', text="Col1")
    tree.heading('Col2', text="Col2")
    tree.heading('Col3', text="Col3")

    tree.tag_configure('even', background="#e8e8e8")

    tree.pack()

    tree.insert("", "end", values=(1, 2, 3))
    tree.insert("", "end", values=(4, 5, 6), tags=('even',))
    tree.insert("", "end", values=(7, 8, 9))
    tree.insert("", "end", values=(10, 11, 12), tags=('even',))
    tree.insert("", "end", values=(13, 14, 15))
    tree.insert("", "end", values=(16, 17, 18), tags=('even',))
    tree.insert("", "end", values=(19, 20, 21))

在此处输入图像描述

This can be done with a ttk.Style :

style = ttk.Style(root)
style.map('my.Treeview', background=[], foreground=[])

tree = ttk.Treeview(root, style='my.Treeview', ...)

If instead of removing the selection color, you want to change it:

style.map('my.Treeview', background=[('selected', bgcolor)], 
          foreground=[('selected', fgcolor)])

Also, if you loose some other effects after doing this (eg the disabled background color), you can look at what was the mapping before you change it:

>>> style.map('Treeview')

{'foreground': [('disabled', '#a3a3a3'), ('selected', '#ffffff')],
 'background': [('disabled', '#d9d9d9'), ('selected', '#4a6984')]}

and add back the part about the disabled colors in the mapping list:

style.map('my.Treeview', background=[('disabled', '#d9d9d9')], foreground=[('disabled', '#a3a3a3')])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM