Images as buttons on the screen

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): 2.6
Standard Standalone? (y/n) If not then what?: Yes
What are you trying to achieve?:

Hey, guys! Once again asking for you help. I made a task that requires the participant to press ‘1’, ‘2’, ‘3’ or ‘4’ to answer the question and it works fine. Any of those answers will finish the routine, but only one is correct and is stored in a variable. But i believe that if i made 4 “buttons”, one for each number (1, 2, 3 and 4) for the participant to click, it would be much better. I want something that looks like this:

teste

But what i need to know is how to convert the one i’m using right now (keyboard press) into this.

What did you try to make it work?:

I tried making an image for each number, like the ones above. Then i used the image tool on psychopy to try and load the images.

Thing is, i already had an image for my stimulus on the same routine and it looks like i can’t load another one, since the program closes as soon as it opens when i try to run it.

Then i tried another approach, but without the button images. I used the text tool and simply wrote ‘1’ as the text. It worked, the ‘1’ shows up on the screen, but when i made another text, i didn’t load (same problem as before).

So this is the urgent topic, to make the buttons appear on the screen, but after that i need to make sure the program not only reads the right answer variable, but saves the participant’s response.

To the best of my knowledge, i need to use the “mouse” response to do this, but i don’t know how to tell the program “between these 4 images, if the stimulus is X, then the correct answer is the second image from left to right, but the participant can click on any of them to finish the routine”

Please help me and thank you in advance! If you need any more info, please let me know!

Instead of pictures, I would make four circle shapes and a text box with the numbers on top of them. Then, you make a mouse component and a code component. In the code component, write:

#figure out which button they pressed
if mouseComponent.isPressedIn(button1) : #button1 is the name of your first circle shape
    theirResponse = 1
elif mouseComponent.isPressedIn(button2) : 
    theirResponse = 2
elif mouseComponent.isPressedIn(button3) : 
    theirResponse = 3
elif mouseComponent.isPressedIn(button4) : 
    theirResponse = 4

#figuring out if they were right or wrong
if theirResponse == theAnswer : #theAnswer is the correct answer for that trial, which should be set up before the trial begins
    accuracy = 1
else :
    accuracy = 0

thisExp.addData("theirResponse", theirResponse)
thisExp.addData("accuracy", accuracy)

Hope this helps!

Tyvm, Sal!

The circles worked great! But after i put the code component, the program started to just open and then instantly close without an error message.

I put your code on Begin Routine and added this line at the beginning of the code: theAnswer = RESPOSTARMexemplo. (which is the variable name for the correct answer on the excel file for this loop). I don’t think this is the right way to set up the correct answer in this case, but even when i put a number on it (eg. theAnswer = 1), it still just opens then closes without an error message. I took out the full line, but no luck.

In any case, i don’t think this is right way to make the code load the correct message from the excel file, can you also help me with that?

This is what the routine looks like right now:

Just to be clear: Per se, there should be no issue with adding additional image components. To figure out what is not working, you might want to send the experiment to the Runner and check Stdout for any error messages you get.

If you search the forum for mouse feedback or mouse correct answer, you’ll probably come across a number of useful suggestions.

Jan

Thank you Jan!

Searching the forum i ended up figuring out most of it. The program works, but i’m having trouble defining one variable. Here’s the code:

#figure out which button they pressed
if mouse_resp_RM.isPressedIn(button1) : #button1 is the name of your first circle shape
    theirResponse = 1
elif mouse_resp_RM.isPressedIn(button2) : 
    theirResponse = 2
elif mouse_resp_RM.isPressedIn(button3) : 
    theirResponse = 3
elif mouse_resp_RM.isPressedIn(button4) : 
    theirResponse = 4

#figuring out if they were right or wrong
if theirResponse == RESPOSTARMexemplo: #theAnswer is the correct answer for that trial, which should be set up before the trial begins
    accuracy = 1
else :
    accuracy = 0

thisExp.addData("theirResponse", theirResponse)
thisExp.addData("accuracy", accuracy)

Where RESPOSTARMexemplo is the correct answer on my excel file. When i run it like this, it tells me theirResponse is not defined, which is true, but when i tried to create a dummy theirResponse = 'Dummy', it worked, but the variable does not seen to change the value when i click the buttons.

I added

print(theirResponse)
print(RESPOSTARMexemplo)

at the beginning of the code and confirmed this, as you can see here:

## Running: C:\Users\anton\OneDrive\Documentos\Programa BVA PsychoPy\rmexteste_lastrun.py ##
3583.0808     INFO     Loaded monitor calibration from ['2020_11_19 18:07']
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Dummy
2
Dummy
2
Dummy
2
Dummy
2
Dummy
2
1.4781     WARNING     We strongly recommend you activate the PTB sound engine in PsychoPy prefs as the preferred audio engine. Its timing is vastly superior. Your prefs are currently set to use ['sounddevice', 'PTB', 'pyo', 'pygame'] (in that order).
##### Experiment ended. #####

RESPOSTARMexemplo is fine, it is indeed 2. But i don’t know what i’m doing wrong since theirResponse isn’t changing it’s value.

Those are the components on the routine to help explain the variable names in the code:

And here is my mouse component if it helps in any way:

You don’t need custom code to figure out which stimulus was clicked: you’ve selected that only valid clicks will be registered and specified the list of valid clickable stimuli. This means that the mouse will have an attribute called .clicked_name, which is a list containing the name of the clicked stimulus.

Michael, thank you for your reponse and i’m sorry but i’m REALLY new to coding, so i don’t get what you mean :’(

It might help to have a look at this example from @Michael.

Jan

I’m suggesting you don’t need to do any of this stuff:

The mouse component will automatically store for you what stimulus name was clicked (because you have told it to do exactly that by providing the list of clickable stimuli).

So in the “end routine” tab, you just need code that does something like this:

if RESPOSTARMexemplo is in mouse_resp_RM.clicked_name:
    accuracy = 1
else:
    accuracy = 0

thisExp.addData("theirResponse", mouse_resp_RM.clicked_name[0])
thisExp.addData("accuracy", accuracy)

Note that the clicked name will correspond to the names of the stimuli, like 'button1', 'button2', etc. So either the variable you use to compare against has to look like that, or you need to extract just the last character of the clicked name (i.e. just the number) to compare against. If so, you need to be careful about the types of your variables. The integer value 1 won’t equal the character '1', for example, so you might have to coerce one or the other using str() or int() to make the comparisons work.

2 Likes

Thank you, it worked!

You guys are awesome, thank you so much!