Change button color after multiple clicks

Hello,

I want to change the color of a button if the user presses on it to and from the original color.

Let’s say the original color is ‘darkgray’.
If the user presses on it once, it turns in ‘blue’.
If the user presses on it again, it turns from ‘blue’ to ‘darkgray’ again and so on and so forth.

I select colors in this format and not RGB (e.g. [0,0,0]) because I think it will be easier to convert to JS for the online one.

I wrote the below code so far, but it only works for the first click (turning the color from darkgray to blue, but not from blue back to darkgray again):

clickedN=0
for clickable in clickables:
    if mouseButton.isPressedIn(clickable):
        clickable.fillColor = 'blue'
        clickedN += 1
    elif mouseButton.isPressedIn(clickable) and clickable.fillColor == 'blue':
        clickable.fillColor == 'darkgray'
        clickedN += 1
        
if clickedN==1:
    continueRoutine == True

I also found the following resource here:

I hence tried adapting my code as such:

if mouseButton.getPressed()[0] == 0:
    fills = [polygonButtonYES.fillColor, polygonButtonNO.fillColor]

for clickable, index in enumerate([polygonButtonYES, polygonButtonNO])
    # If mouse pressed in square, match current square to square stored in fills list
    # Check if the colour has not changed. If it has not changed, change the colour of the square 
    # that contains the mouse click 
    if mouseButton.isPressedIn(clickable) and fills[index] == 'darkgrey':
       clickable.fillColor = 'blue'
    elif mouseButton.isPressedIn(clickable) and fills[index] == 'blue':
       clickable.fillColor = 'darkgrey'

But I receive this error:

Alert 4205:Python Syntax Error in 'Each Frame' tab. See 'for clickable, index in enumerate([polygonButtonYES, polygonButtonNO])
' on line number 7 of the 'Each Frame' tab.

Any ideas on how to solve this?
Can this be done using the buttonStim directly and not using “Polygon + Mouse” ?
Thanks!

Your loop condition is much more complicated than the original version. With just two buttons you could deal with them separately. What does index mean above? Do you have several of each button?

The index is used to indent the two polygons corresponding to each of the buttons.

So yes, the index should refer as in the original version to either of the two buttons.- in my case, it’s the YES and NO buttons.
I don’t see why the loop conditions is much more complicated - it’s basically the same, I just replaced some naming conventions, that’s all.

I will try to deal with each button separately.

Do you know if I could do this using a buttonStim instead of (polygon+mouse+text) ?

Thanks for the fast response!

Buttons don’t work online.

I’m not sure about two dimensional loops.

I’m not sure about enumerate. That might be fine

For me, buttons work online and can be synchronized with Pavlovia (I just tested and it works).

Do you know the condition for the button to be pressed?

I’m trying the following “IsPressedIn()” or “GetClicked()” , but they are not working:

For example:

if(buttonYES.getClicked() is not None):
    buttonYES.fillColor='blue'

if(buttonYES.fillColor=='blue'):
    buttonYES.fillColor='darkgrey'

I obtain the following error message (as expected)
AttributeError: 'ButtonStim' object has no attribute 'getClicked'

And I cannot find more information here or elsewhere:
https://psychopy.org/builder/components/button.html

Thanks,
Mihai

If buttons work in the latest version, then can’t you put the conditional code within the button if you keep it simple?

I tried doing that, but then it doesn’t switch back to darkgrey.

I have this:

After clicking once - it does turn blue, but it does not turn back afterwards.

I tried the following, but it does not work:

…as I assume that the condition to click the button is already there, but I don’t know it.