psychopy.org | Reference | Downloads | Github

Colored Feedback on a Buttonbox after clicked

PsychoPy version: PsychoPy v2020. 1.3

Hi,

I would like to create a routine with a question (textelement) and two different answer options. For each option, I use a textelement with a buttonbox. The participants should response by clicking on one of the buttonboxes. After a participant have clicked (mouse element) on one of the buttonboxes for the answer, the buttonbox should change the color (for example from white to black) for a colored feedback.

How can I implement a colored feedback like this? Additionally, it should only be possible to select one option as the answer. So only one box should turn black at a time, and if the participant changes his answer, the box would turn white again.

Thank you in advance!

Hi Hanna,

To do this I would use a custom code component with some code in the Each Frame tab to:

  1. Check whether the button box has been clicked, and
  2. if it has, recolour the box.

Is this the same experiment you were doing before, with the polygons and mouse components? If so it would look something like this:

if BOXNAME.name in MOUSENAME.clicked_name: # If box name is in list of boxes clicked by the mouse component
    BOXNAME.color = [1,1,1] # Set the colour of the box to be white

Yes it is the same experiment like I were doing before, so I have tried this out but unfortunately the color didn’t change…

Do I maybe have to change something in the box settings?
Here you can see how it looks right now:

Ah, my mistake, I misread. If you want it to turn from white to black then you need to change the value you’re setting BOXNAME.color to, so it’s [0,0,0] (a.k.a. black).

To make it change back, which I forgot originally, you need to essentially say “if there has been an odd number of clicks, the box is black, if there has been an even number of clicks, it is white”, so:

matches = [BOXNAME.name in clicked for clicked in MOUSENAME.clicked_name]  # Get boolean array of previous clicks which match clicks on this box
if sum(matches)%2: # If the remainder from dividing number of matches by 2 is 1 (i.e. if there are an odd number of matches)
    BOXNAME.color = [0,0,0] # Set box colour to black
else:
    BOXNAME.color = [1,1,1] # Set box colour to white

You can change the colours as you need, the three numbers are just how much red, blue and green are in the colour respectively (1 is max, 0 is min, e.g. [1,0,0] is pure red).

Ok thank you! Now for the first button it works great, but when I have already clicked on the first button and then want to do a change and click on the second button, no color change appears for the second button. And when I click on the second button at first, both bottons get blue at the same time. Do you know how to solve this?

I used the following code:

matches = [Deutsch.name in clicked for clicked in Maussprache.clicked_name]
if sum(matches)%2:
Deutsch.color = [0,0,1]
else:
Deutsch.color = [1,1,1]

matches = [KeinDeutsch.name in clicked for clicked in Maussprache.clicked_name]
if sum(matches)%2:
KeinDeutsch.color = [0,0,1]
else:
KeinDeutsch.color = [1,1,1]

That’s strange… Could you amend the code to the following and then show me what gets printed?

matches = [Deutsch.name in clicked for clicked in Maussprache.clicked_name]
if sum(matches)%2:
Deutsch.color = [0,0,1]
else:
Deutsch.color = [1,1,1]
print('Matches1', matches)

matches = [KeinDeutsch.name in clicked for clicked in Maussprache.clicked_name]
if sum(matches)%2:
KeinDeutsch.color = [0,0,1]
else:
KeinDeutsch.color = [1,1,1]
print('Matches2', matches)
print("Deutsch", Deutsch.name, "KeinDeutsch", KeinDeutsch.name, "Mouse", Maussprache.clicked_name)

I think the problem will be in what is being stored as Deutsch.name, or in Maussprache.clicked_name, so seeing what it’s comparing will give me a better idea of what’s going wrong.

Hi Todd.

Are you sure that 0 is minimum rather than -1? Is [0,0,0] black or mid grey?

Best wishes

Wakefield

Ah, that’s true yes! [0, 0, 0] is mid-grey, black is [-1, -1, -1]. It doesn’t explain the problem with not registering clicks, but it should be [-1,-1,1] for pure blue. Although [0, 0, 1] is quite a nice shade anyway!

I found a solution to solve the problem.

I used the following code in each frame:

if(len(Maussprache.clicked_name) >= 1):
    if(Maussprache.clicked_name[-1] is Deutsch.name):
        Deutsch.color =  [0,0,1]
        KeinDeutsch.color =[1,1,1]
    
    elif(Maussprache.clicked_name[-1] is KeinDeutsch.name):
        Deutsch.color = [1,1,1]
        KeinDeutsch.color = [0,0,1]
        
    elif(Maussprache.clicked_name[-1] is nexxt_2.name):
        continueRoutine = False

In my mouse component I used the following settings:

Thank you very much for your help!