psychopy.org | Reference | Downloads | Github

Selecting images via mouse click with visual feedback

Win10
PsychoPy3 (v2020.1.3)
Standard Standalone? (y/n) yes
What are you trying to achieve?:
I have 42 images, which I want to show one by one in a set order. The participant has to selcect with each image, if the image belongs to the indicated category by clicking on the image. So “yes” means clicking on the image and “no” means clicking on a small text button under the image (like “No, next”). I want to show a feedback to the participant, so I want the image to turn green if they click on it. This selection should be able to undo. Best case scenario, all clicks and undone clicks should be stored in the csv file and also be compared to a list which has the correct answers for each image.

This is my code component so far:
My Mouse component ist called “mouse_Indiv”

Beginn Experiment:

# Add all your image variables to a list

imagesIndividual = [image_Folie37, image_Folie2, image_Folie33, image_Folie8, image_Folie38, image_Folie18, image_Folie21, image_Folie4, image_Folie14, image_Folie42, image_Folie31, image_Folie13, image_Folie27, image_Folie34, image_Folie7, image_Folie20, image_Folie15, image_Folie29, image_Folie32, image_Folie1, image_Folie22, image_Folie23, image_Folie36, image_Folie11, image_Folie12, image_Folie25, image_Folie40, image_Folie10, image_Folie6, image_Folie28, image_Folie19, image_Folie41, image_Folie24, image_Folie9, image_Folie35, image_Folie16, image_Folie5, image_Folie39, image_Folie3, image_Folie26, image_Folie17, image_Folie30]

Each frame:

for stimulus in [image_Folie37, image_Folie2, image_Folie33, image_Folie8, image_Folie38, image_Folie18, image_Folie21, image_Folie4, image_Folie14, image_Folie42, image_Folie31, image_Folie13, image_Folie27, image_Folie34, image_Folie7, image_Folie20, image_Folie15, image_Folie29, image_Folie32, image_Folie1, image_Folie22, image_Folie23, image_Folie36, image_Folie11, image_Folie12, image_Folie25, image_Folie40, image_Folie10, image_Folie6, image_Folie28, image_Folie19, image_Folie41, image_Folie24, image_Folie9, image_Folie35, image_Folie16, image_Folie5, image_Folie39, image_Folie3, image_Folie26, image_Folie17, image_Folie30]:
    if mouse_Indiv.isPressedIn(stimulus): 
        stimulus.setOpacity(.9)  # fade image to show it was clicked
        stimulus.setColor = 'green'
  
        #if mouse_Indiv.isPressedIn(stimulus) == 1:
            #stimulus.setOpacity.reset(0)
            #stimulus.stim.color.reset
          
    for clicked in mouse_Indiv.clicked_name:  
    # Check the list of clicked images
        if clicked.imagesIndividual in [corrAnsIndiv]:  
        # is the clicked image name in the list of correct answers
            response = 1  # 1 for correct
        else:
            response = 0  # for incorrect
        thisExp.addData("responseCorrIndiv", response)  
    # store the correct answer in your datafile
    
    # end the trial once done here:
        continueRoutine = False

    # stop any further checking:
        break 

I’m getting an error message in the “Each frame” Tab with setting the color, reseting the click &color again and the last “for …” python code.
The problems:

  1. After clicking on the first image the experiment crashes without ever turnig green and an error message appears, e.g.:
    if clicked.imagesIndividual in [corrAnsIndiv]:  
AttributeError: 'str' object has no attribute 'imagesIndividual'
##### Experiment ended. #####

(I’ve changed “imagesIndividual” to “stimulus” or “images” but nothing works…)

  1. The first image shown ist not the one I set to be the first one, although I selected “sequential” in the Routine Loop.

Pretty sure the Python code snippet is somehow wrong but I have tried everything I can think of and read so so many articles in the PsychoPy forum but nothing really changes in the long run. Please help me, I’m at a loss here!

Where did you get the information that the mouse object has a clicked_name attribute? I couldn’t find that in the documentation nor do you set it in the code you posted. I don’t know why your experiment doesn’t throw an error on this line.

With the line for clicked in mouse_Indiv.clicked_name you’re iterating over whatever your mouse_Indiv.clicked_name contains. The error tells you that clicked is of type string, so mouse_Indiv.clicked_name seems to be a list of strings. A string doesn’t have an attribute imagesIndividual.

How did you configure your loop? Are you using an xlsx or csv file? And how do you determine which image to show?

That is not how you set the color. You have two options (with the first one being the preferred syntax):

stim.color = 'green'
stim.setColor('green')

Is your experiment time-sensitive? Then I don’t suggest looping over all of your image stimuli like this:

for stimulus in [image_Folie37, image_Folie2, image_Folie33, image_Folie8, image_Folie38, image_Folie18, image_Folie21, image_Folie4, image_Folie14, image_Folie42, image_Folie31, image_Folie13, image_Folie27, image_Folie34, image_Folie7, image_Folie20, image_Folie15, image_Folie29, image_Folie32, image_Folie1, image_Folie22, image_Folie23, image_Folie36, image_Folie11, image_Folie12, image_Folie25, image_Folie40, image_Folie10, image_Folie6, image_Folie28, image_Folie19, image_Folie41, image_Folie24, image_Folie9, image_Folie35, image_Folie16, image_Folie5, image_Folie39, image_Folie3, image_Folie26, image_Folie17, image_Folie30]:

A simple fix I could think of from the top of my head (but there are surely better ways), is to put an invisible shape stim where all of your images go and then check for clicks in this shape stim instead of looping over all possible images.

1 Like