Text color in forms

I’ve been customizing the Override V/G tool, but can’t figure out how to change the colour of the text in the form that pops up. Here’s what I’ve got:

While the color of the text does change to white when moused over, I would like to make the text on the dark buttons white or yellow in the non-mouseover state, i.e. like this:

The relevant code is

selected_switch = \
        config={'1 make RED': {'background': COLOUR_RED}},
        message='Pick Visibility/Graphics override option:'

[“COLOUR_RED” is a constant defined elsewhere.]

I assume there’s a property named ‘color’ or ‘text’ or ‘fontcolor’ or something similar that I can set to a colour of my choice, but after reading pyrevit.forms — pyRevit 4.5 documentation I can’t seem to find it.

Thanks for any suggestions.

what happens when you try and set foreground color.

Hi JP, thanks for your suggestion. Unfortunately, nothing happens :frowning: The relevant line of code, which works (it makes the background red), is

'1 make RED': {'background': COLOUR_RED},

so I’ve tried each of these variations (one at a time of course!):

'1 make RED': {'background': COLOUR_RED, 'foreground' : COLOUR_WHITE}
'1 make RED': {'background': COLOUR_RED, 'foreground color' : COLOUR_WHITE}
'1 make RED': {'background': COLOUR_RED, 'foregroundcolor' : COLOUR_WHITE}
'1 make RED': {'background': COLOUR_RED, 'color' : COLOUR_WHITE}
'1 make RED': {'background': COLOUR_RED, 'font color' : COLOUR_WHITE}
'1 make RED': {'background': COLOUR_RED, 'fontcolor' : COLOUR_WHITE}

… and none of those has any effect (well, the background is still red, but the text doesn’t change).

I’m assuming this is a PyRevit issue – either a lack of documentation or perhaps the functionality of changing the text colour just isn’t available. If it can’t be done, it’s not a deal-breaker, but I’d just like to know!

Open the xaml file for command switch and play with value on line 17. Haven’t tried but seems like that might work.

Or instead of editing xaml edit whatever pyRevitDarkerDarkBrush is referencing

1 Like

Thank you, but I think this is where I have to stop now. :frowning:

My code tweaking so far has been limited to opening up the script.py file associated with a particular PyRevit function, and editing that. After reading your suggestion I searched my computer for “pyrevit xaml” and found quite a few scripts, then googled those same terms and found a much deeper rabbit hole than I had expected. I’d figured it would be a relatively straightforward matter of looking up the syntax of the parameter I wanted to change, then implementing that.

I may eventually find time to start studying xaml, but for now I’ll have to curtail my PyRevit ambitions…

Thanks for the answer, though.

1 Like

That was it on line 17.

The original line was
<Setter Property="Foreground" Value="{DynamicResource pyRevitDarkerDarkBrush}"/>
and that was preventing overriding the color.

I wasn’t able to figure out how to change the colors for foreground or background within the script, only by modifying the XAML directly. The only script I had that used that command switch is using some complicated dictionaries, so i probably wasn’t doing correct.

The background that you were able to override in the script looks like this
<Setter Property="Background" Value="#ffffff"/>

so if you change the dynamic resource on line 17 to match, that may allow you to override as well.

Once I located the CommandSwitchWindow.xaml file, I finally understood what you were referring to :slight_smile: And I was able to change the text colour – but only by hardcoding it within the xaml file. For some reason the script.py’s override works for the background but is ignored for the foreground.

So these lines in xaml, by themselves, will make both the text and background cyan:
But if I then try to override those from within the script.py file, with this code:

'6 LINES red' : {'background' : COLOUR_RED, 'foreground' : COLOUR_RED},

… then only the background colour actually changes to red, and the foreground stays cyan. And it doesn’t seem to be an uppercase/lowercase issue, as it happens whether I use ‘foreground’ and ‘Foreground’ in my code.

For what it’s worth, I also tried changing the values of “BorderBrush” and “BorderThickness”, which exhibited the same behaviour – i.e. I could hardcode a different value into the xaml (just like ‘Background’ is hardcoded), but trying to change them from within the script.py file wouldn’t work. It’s as if there’s code somewhere else that is enabling the background attribute to be mutable, but not the others :confused:

I may have figured it out. Can I see the code you are using for the command switch so I can test it?

So here are lines 14 through 17 inside \AppData\Roaming\pyRevit-Master\pyrevitlib\pyrevit\formsCommandSwitchWindow.xaml before any change:

            <Setter Property="Background" Value="#ffffff"/>
            <Setter Property="BorderBrush" Value="#000000"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Foreground" Value="{DynamicResource pyRevitDarkerDarkBrush}"/> 

and after:

            <Setter Property="Background" Value="#ff0000"/>
            <Setter Property="BorderBrush" Value="#ffff00"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="#00ff00"/> 

The relevant code in the script.py file is this:

selected_switch = \
        options.keys(), # this just tailors visual appearance for various of the 'options' listed above;
                        # any items from the 'options' missing from the following list will simply appear with the default white background
        config={'Halftone Selected': {'background': '0xaaaaaa'},
                '1 make RED': {'background': COLOUR_RED, 'font color': COLOUR_WHITE},
                '2 make YELLOW': {'background': COLOUR_YELLOW},
                '3 make GREEN': {'background': COLOUR_GREEN},
                '4 make BLUE': {'background': COLOUR_BLUE},
                '5 make FUCSIA': {'background': COLOUR_FUCSIA},
                '6 LINES red' : {'background' : COLOUR_BLUE_DARK, 'BorderBrush' : COLOUR_FUCSIA, 'BorderThickness' : '3', 'Foreground' : COLOUR_WHITE},
                '7 LINES blue' : {'background' : COLOUR_BLUE_MEDIUM},
                '8 make EXISTING (grey)': {'background': COLOUR_GREY},
                '9 make HALFTONE': {'background': COLOUR_HALFTONE}},
        message='Pick Visibility/Graphics override option:'

It’s that longest line that I’m experimenting with. If I comment it out, I get this:

If I uncomment it, I get this:
… where you can see that the only part of my script.py code that changed the properties defined in the xaml file is the background.

If you want the entire script.py file let me know.

Thanks for your help!