# Change color of polygon when mouse is clicked

**URL:** <https://discourse.psychopy.org/t/change-color-of-polygon-when-mouse-is-clicked/28208>\
**Category:** Builder\
**Created:** [March 25, 2022, 11:21am UTC](https://discourse.psychopy.org/t/change-color-of-polygon-when-mouse-is-clicked/28208 "2022-03-25T11:21:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![M11](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/m11/32/4334_2.png) [@M11](https://discourse.psychopy.org/u/M11)\
**Post date:** [March 25, 2022, 11:21am UTC](https://discourse.psychopy.org/t/change-color-of-polygon-when-mouse-is-clicked/28208/1 "2022-03-25T11:21:58Z")

</div>

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](https://discourse.psychopy.org/t/how-to-make-polygons-change-color-when-pressed/19282) 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:

```auto
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)

```auto
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
```

---

<div class="post-metadata">

**Author:** ![TParsons](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/tparsons/32/8301_2.png) [@TParsons](https://discourse.psychopy.org/u/TParsons)\
**Post date:** [March 26, 2022, 1:52pm UTC](https://discourse.psychopy.org/t/change-color-of-polygon-when-mouse-is-clicked/28208/2 "2022-03-26T13:52:10Z")

</div>

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”
