How does one go about changing the selected text color in treeview, I can't seem to find much on the subject.
Here is what I have tried but the color doesn't change to red as I would like, it stays blue.
from tkinter import *
from tkinter.ttk import Treeview, Style
class App(Frame):
def __init__(self, parent):
super().__init__()
self.container = Frame.__init__(self, parent)
style = Style()
self.tv = None
self.tree()
style.configure('Treeview', selectbackground='red')
def tree(self):
tv = self.tv = Treeview(self.container)
tv.grid(sticky='NSEW')
tv.insert('', '0', 'item1', text='Item 1')
tv.insert('', '1', 'item2', text='Item 2')
tv.insert('', '2', 'item3', text='Item 3')
tv.insert('item1', '0', 'python1', text='Python 1')
tv.insert('item1', '1', 'python2', text='Python 2')
tv.insert('python1', '0', 'sub1', text='Sub item 1')
tv.insert('python1', '1', 'sub2', text='Sub item 2')
def main():
root = Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
App(root)
root.mainloop()
if __name__ == '__main__':
main()
The Treeview widget items can be edited and deleted by selecting the item using tree. selection() function. Once an item is selected, we can perform certain operations to delete or edit the item.
You can use a string specifying the proportion of red, green and blue in hexadecimal digits. For example, "#fff" is white, "#000000" is black, "#000fff000" is pure green, and "#00ffff" is pure cyan (green plus blue). You can also use any locally defined standard color name.
We can set the background color, foreground color, and other attributes of the Combobox widget by visiting the configure function in ttk and passing 'TCombobox' as the first parameter.
If we want to remove or clear all the items in a given treeview widget, then we have to first select all the items present in the treeview widget using get_children() method. Once we have selected all the treeview items programmatically, then we can delete the items using delete(item) method.
The selected background color is not set with a selectbackground
option but as a dynamic value of the background
option. Therefore to set this option you need to replace
style.configure('Treeview', selectbackground='red')
by
style.map('Treeview', background=[('selected', 'red')])
Which means that when the item is in 'selected' state, its background is red. This can also be used to set a disabled background color for instance.
You can find more information about dynamic appearance change here: https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/ttk-map.html
You can also query the current dynamic values with style.map('Treeview')
or style.map('Treeview', 'background')
(to get only the list of values for the background).
By the way, as suggested by stovfl, if you also need to change the colors of specific rows, you can have a look at Unable to change background color of treeview in python.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With