Change color of polygon when mouse is clicked

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10):
PsychoPy version (e.g. 1.84.x): 2022.1.1.

Hi!
Sorry for another post on changing stimulus color. I read this very helpful post: How to make polygons change color when pressed on the change of a polygons’ color when the mouse is clicked on that polygon.
I created a simple white rectangle. When I click on the rectangle it changes its color to red. This works fine with the following code:

if mouse.isPressedIn(rectangle):
    rectangle.fillColor = 'red'

However, when I try to implement the possibility that the red rectangle becomes white again when I press it another time the following code does not work. The red rectangle stays red (without flickering). I know that a flickering stimulus is another problem but I want to basically understand what is wrong with my code (I also tried the code of Becca in the post above but this one also does not work)

if mouse.isPressedIn(rectangle):
        if rectangle.fillColor =='red':
            rectangle.fillColor = 'white'
        else:
            rectangle.fillColor = 'red'

Thank you for any help on this issue!

Best, M

You need to de-indent the else - currently you’re saying “if the fill colour is red, make it white, and if it’s not red, make it red”

1 Like