Mouse click counter

Hi everyone! I am a beginner in using the code component in Psychopy and need your help.

What are you trying to achieve?:

I want to create a mouse click counter for each of the text components on the screen. If the # clicks are odd the text will turn green, otherwise white.


image

What did you try to make it work?:

I have created a dataframe that contains text components and corresponding click counters starting from 0.

What specifically went wrong when you tried that?:
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\event.py”, line 877, in isPressedIn
return any(wanted & pressed) and shape.contains(self)
AttributeError: ‘str’ object has no attribute ‘contains’

OS (Win10):
**PsychoPy version 2.4

Hi @berna,

Based on this error you are receiving, it looks like on line 11 (where you call isPressedIn) the variable x is being treated as a string (str) instead of as a shape. I think Pandas DataFrames only allow 1 datatype in the dataframe, so you are losing important type information by mixing shapes (clickables) and integers (clickcount). I would try using code that doesn’t involve Dataframes:

In Begin Routine you initialize your variables:

clickables = [ans1, ans2, ans3]
clickcount = [0] * len(clickables) #the number of 0s in clickcount should match the number of clickables

In Each Frame you check your click status:

for i, x in enumerate(clickables): # i = index within clickables list, x = a shape from clickables
  if mouse_5.isPressedIn(x):
    clickcount[i] += 1
    if clickcount[i] %2 == 1:
      x.color = 'green'
    else:
      x.color = 'white'

enumerate() gives you an index,value (i,x) pair so that you can use your shape x but also match that to the number in clickcount with the index i. Enumerate’s documentation

Note: isPressedIn will constantly activate if the mouse is held down, so you might also need code to track the mouse state if that behaviour is not what you want. This thread has some example code for tracking mouse state.

I tested this in PsychoPy 2022.2.4. Other versions may behave differently.

Hope this helps,
-shabkr