tkinter ttk.Treeview: changing background colour of single item when selected

stefalem stefalem79 at gmail.com.invalid
Sun Feb 12 06:31:44 EST 2023


Il 12/02/23 12:10, John O'Hagan ha scritto:

 > My goal was to be able to change the colour of an individual item
 > regardless of whether it is selected or not. To do that, it is
 > necessary to be able to change the colour of an individual selected
 > item, without changing the selection or changing the colour of other
 > selected items. It seems this isn't possible.

ok sorry. As another alternative I had thought of this:

from tkinter import *
from tkinter.ttk import *

root = Tk()
t = Treeview(root)

t.insert('', 0, iid='item1', text='item1')
t.insert('', 1, text='item2')
t.tag_configure('flashtag', background='red')
t.pack()
style = Style()
styleDefault = (style.map("Treeview"))

def flash():
  tags = t.item('item1', 'tags')
  t.item('item1', tags='' if tags else 'flashtag')
  t.after(500, flash)
  itemselected = t.selection()
  for x in itemselected:
   if (x == 'item1'):
    style.configure('Treeview', selectbackground='red')
    style.map('Treeview', background=[('disabled', 'white')], 
foreground=[('disabled', 'red')])
   else:
    style.map('Treeview', background=[('selected', 
styleDefault['background'][1][1])],
                          foreground=[('selected', 
styleDefault['background'][0][1])])
flash()
mainloop()

Maybe it can be useful for other situations.

Bye bye


More information about the Python-list mailing list