Getting the name of the component with mouse click

Hi,

When I click on something with my mouse, I would like to retrieve the name of the component.
The components are polygon stimuli with names like circle, square etc.

If I try to retrieve the name as mouse.name, I get the following error:
AttributeError: ‘Mouse’ object has no attribute ‘name’

I even tried mouse.name[0]. Same error.

Under mouse, I have : Store params for clicked $name,

Screen Shot 2020-12-28 at 1.59.20 AM

How can i retrieve the name?such as circle or triangle depending on which polygon stimuli is clicked?
Screen Shot 2020-12-28 at 2.00.29 AM

OS (e.g. Win10): mac sierra
PsychoPy version (e.g. 1.84.x): 2020.2.3
Standard Standalone? (y/n) If not then what?: build scratch

Hi @Irfa_Nisar, try mouse.clicked_name which will return a list of clicked objects.

This was the first thing i tried: mouse.clicked_name[0].
Same kind of error,
AttributeError: ‘Mouse’ object has no attribute ‘clicked_name’

You need to supply the names of the relevant stimuli in the “Clickable stimuli” field (separated by commas if there is more than one). The attribute .clicked_name is only created in a Builder script if that has been done, because otherwise it doesn’t need to exist.

Now from your post it seems that you have done that, so I guess this might be because the code is running before the .clicked_name attribute has been added as an empty list. This is likely because your code is running at the wrong time. .clicked_name is created at the beginning of the routine - check if your code component is above the mouse component and is running in the “Begin routine” tab (logically, it should probably be in the “each frame” or “end routine” tab anyway).

It is a bit unusual that this attribute is dynamically added to the mouse object in the script, rather than being a fixed part of the definition of the Mouse class itself. That is also maybe reflected in it being written in snake_case, rather than the camelCase that is usual for PsychoPy class attributes.

I do have “circle,triangle,” in the clickable stimuli field box and they map to stimuli that are polygons. “circle” is the name of that stimuli. Likewise, triangle is the name of the other stimuli.

Thanks, it was supposed to go in the end routine section. Working now :slight_smile: