Tk Source Code

View Ticket
Login
Ticket UUID: 4736b9f290d787f23338c2c6ce337894967bdca2
Title: Bug in tk_setPalette in connection with TIP 645 ("ttk color palette support")
Type: Bug Version: trunk
Submitter: nemethi Created on: 2023-07-30 15:14:40
Subsystem: 60. [tk_setPalette] Assigned To: fvogel
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2023-08-03 20:04:45
Resolution: Fixed Closed By: fvogel
    Closed on: 2023-08-03 20:04:45
Description:
As part of the implementation of TIP 645 ("ttk color palette support"), the body of the library procedure tk_setPalette was extended by the following lines:

    # Update the 'default' ttk theme with the new palette,
    # and then set 'default' as the current ttk theme,
    # in order to apply the new palette to the ttk widgets.
  
    foreach option [array names new] {
        if {[info exists ttk::theme::default::colorOptionLookup($option)]} {
            foreach colorName $ttk::theme::default::colorOptionLookup($option) {
                set ttk::theme::default::colors($colorName) $new($option)
            }
        }           
    }               
    ttk::theme::default::reconfigureDefaultTheme
    ttk::setTheme default

This change has some undesired effects if tk_setPalette is invoked when the current theme is different from "default".  For example, when changing the theme from "default" to "sun-valley-light" or "sun-valley_dark", tk_setPalette gets automatically invoked (in order to apply the new color palette to the non-ttk widgets, too), and this has two unexpected effects:

1. The theme gets set back to "default" (which is quite annoying);

2. Both the currently existent widgets and the ones created later appear in the colors specific to "sun-valley-light" or "sun-valley_dark" rather than in the colors of the "default" theme.

To solve these problems, I propose two changes:

1. Activate the above code only if the current theme is "default";

2. Remove the line "ttk::setTheme default".

The code changed according to this proposal reads as follows:

    if {[ttk::style theme use] ne "default"} {
        return
    }   
            
    # Update the 'default' ttk theme with the new palette,
    # in order to apply that palette to the ttk widgets.
        
    foreach option [array names new] {
        if {[info exists ttk::theme::default::colorOptionLookup($option)]} {
            foreach colorName $ttk::theme::default::colorOptionLookup($option) {
                set ttk::theme::default::colors($colorName) $new($option)
            }   
        }   
    }       
    ttk::theme::default::reconfigureDefaultTheme

I have successfully tested that this fix proposal eliminates the issues experienced in connection with the above-mentioned themes.
User Comments: fvogel added on 2023-08-01 19:36:22:

Thanks! Committed in [bc842ffe] and merged in trunk.


nemethi (claiming to be Csaba Nemethi) added on 2023-07-31 12:46:05:
Small addition: Overriding the colors of the "default" theme via the option database is supported on X11 only (see file ttk/defaults.tcl).  Hence the first line of the proposed change should be modified to become:

    if {[tk windowingsystem] ne "x11" || [ttk::style theme use] ne "default"} {

stevel added on 2023-07-31 01:42:58:
I have tested the fix in the ticket on both macOS and Linux and confirms it works, so I can’t see a reason why it shouldn’t be applied.