Updating Button Color ON Click Event

How can I script an On Click event to change the color of the button I clicked?

The button they are clicking should change color after they click it? Is there anything wrong with the onAction event of the button itself?

No, I am have trouble setting up a the script to get this done. I chose a on mouse click event.

What do you have so far?

def runAction(self, event):

def runAction(self, event):
	self.props.style.backgroundColor = "#FF0000"

But note that this only changes it to the color you declare and never changes it back; subsequent clicks will have no effect.

new with ignition. any idea on how to have the button switch back color if click again?

There are lots of ways, but the common strategy is going to be to store the swap color in some prop somewhere. Then, you can either swap between the two colors, determine which color to display based on a click count, or use the current color to determine the next color.

def runAction(self, event):
    custom_color = "#FF0000"
    if self.props.style.backgroundColor == custom_color:
        # assuming you've stored the default color in a custom prop named `defaultColor`
	    self.props.style.backgroundColor = self.custom.defaultColor
    else:
        self.props.style.backgroundColor = custom_color

Using a click count:

def runAction(self, event):
    self.custom.clickCount += 1
    self.props.style.backgroundColor = self.custom.defaultColor if self.custom.clickCount % 2 == 0 else "#FF0000"

Mapping:

def runAction(self, event):
    color_map = {
        self.custom.defaultColor: "#FF0000",
        "#FF0000": self.custom.defaultColor
    }
	self.props.style.backgroundColor = color_map.get(self.props.style.backgroundColor, self.custom.defaultColor)
1 Like