It is possible to change the row selection background color.
In the example above, the row selection background color is green.
We can do this with a custom ttk Style.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
# We will need the column's name later to set the header text.
column_names = "last_name_column"
s = ttk.Style()
# Set the selection background color to green.
s.map("Custom.Treeview", background=[("selected", "green")])
# We now have a custom treeview style called 'Custom.Treeview'.
# We're going to use that style when we instantiate our treeview class below.
treeview_example = ttk.Treeview(root,
columns=column_names,
style="Custom.Treeview")
# Show text in the headers
treeview_example.heading("#0", text="First Name")
treeview_example.heading("last_name_column", text="Last Name")
# Add two rows to the treeview.
treeview_example.insert(parent="", index=0, text="John", values=("Smith",))
treeview_example.insert(parent="", index=0, text="Bob", values=("Jones",))
treeview_example.pack(expand=True, fill=tk.BOTH)
root.mainloop()
How to get the default styles for the Treeview widget
To see what default styles the Treeview widget has, use the .map method.
s = ttk.Style()
print(s.map("Treeview"))
>> {'foreground': [('disabled', '#a3a3a3'), ('selected', '#ffffff')], 'background': [('disabled', '#d9d9d9'), ('selected', '#4a6984')]}
Change row selection color when treeview is disabled
We can also change the selection background color to something else when the entire Treeview widget is disabled.
The treeview widget is disabled. Notice that the row selection color is different. The code to make this happen is below.
We can choose what the selection color is by using the .map() method for ttk Styles.
s = ttk.Style()
s.map("Custom.Treeview", background=[("selected", "disabled", "lightgrey"), ("selected", "purple")],
foreground=[("selected", "disabled", "darkgrey"), ("selected", "white")])
# Here we've told Style: if a row is selected AND the treeview widget is disabled,
# make the selection background color 'lightgrey'. Otherwise, if a row is
# selected, make the background selection color 'green'.
# We've also told Style: if a row is selected AND the treeview widget is disabled,
# make the foreground selection color 'darkgrey'. Otherwise, if a row is
# selected, make the foreground selection color 'lightblue'.
# Disable the treeview widget.
treeview_example.state(["disabled"])
The treeview is no longer disabled
This time, the treeview widget is not disabled. Notice that the selected row has a purple background with white text. That’s because of this line:
s.map("Custom.Treeview", background=[("selected", "disabled", "lightgrey"), ("selected", "purple")],
foreground=[("selected", "disabled", "darkgrey"), ("selected", "white")])
No row selection color
What if we don’t want to make the treeview show a row selection background color? For example, when the treeview is disabled, we may not want the user to assume that it might be enabled, so we’ll prevent changing the row background color when rows are clicked on.
In the screenshot above, the rows will have a lightgrey background color, even if the rows get clicked on with the mouse.
s = ttk.Style()
# When the treeview is disabled, make the row item backgrounds lightgrey,
# even if get selected.
# Similarly, when the treeview widget is disabled, set the foreground color
# to #555753
s.map("Custom.Treeview", background=[("disabled", "lightgrey")],
foreground=[("disabled", "#555753")])
# Disable the treeview widget.
treeview_example.state(["disabled"])