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

John O'Hagan research at johnohagan.com
Mon Feb 13 00:42:03 EST 2023


On Sun, 2023-02-12 at 08:59 -0500, Thomas Passin wrote:

[...]

> On 2/12/2023 6:10 AM, John O'Hagan wrote:

[...]

> > 
> > 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.
> 
> I haven't worked with ttk objects or Treeviews, but judging from old 
> style objects, I think you have to re-apply your color and flashing
> when 
> the item becomes selected and possibly again when it becomes
> unselected.
> 
> Depending on exactly what effect you want, you may also need to apply
> color and flashing when the mouse moves over the item and again when
> it 
> leaves.  When I make changes in e.g. color, I like to save the
> previous 
> value(s) in the object itself.  That way I can easily restore say a 
> background color without having to work out what it used to be, which
> may be some color that Tk applies based on the desktop theme and who 
> know what else.
> 
> Here's an example (simplified) for changing color on mouse hover and 
> leave events:
> 
> BG_KEY = 'bg' if platform.lower().startswith('win') \
>           else 'activebackground'  # Different for Linux!
> 
> def on_enter(event):
>      w = event.widget
>      w.old_bg = w.cget('bg')
>      w[BG_KEY] = BUTTON_HOVER  # Color you have chosen
> 
> def on_leave(event):
>      w = event.widget
>      _bg = w.old_bg
>      w[BG_KEY] = _bg
> 
> 

Thank you again for your reply, Thomas. I didn't know tkinter
configuration keys have different names on different platforms, that
seems unneccesary! I also like the idea of storing the original
settings on the object itself.

However, because of the nature of ttk widgets, I think I'm stuck with
my original issue. Unlike classic tkinter widgets, a lot of the ttk
configuration options are not available on individual widgets, but must
be done through styles or tags. On Treeviews (and possibly other ttk
widgets), the colours for selected items can only be set using
style.map (AFAIK).  The colours for individual items can (only?) be set
using tags, but this is evidently overridden when the item is
selected by the selection colour dictated by the style. Treeview tags
do not have a 'selectbackground' option like many tkinter widgets do.
(Fonts _can_ be set this way, so I could flash, say, the font size, but
that's ugly!)

As as aside, IMO ttk looks great (and Treeview is really useful) but
this approach to configuration is a double-edged sword. On one hand
it's super-easy to change the whole look, but on the other, it can be
overly restrictive if you want to control the look of individual
widgets. 

I might try to put forward a feature request to add a
'selectbackground' option to ttk tags to see if there's any interest.

Thanks

--

John


More information about the Python-list mailing list