| Reference | Downloads | Github

Select letters using mouse (v1.83.04)

OS (e.g. Win10): Windows 10
PsychoPy version 1.83.04
What are you trying to achieve?:
Hi everyone, I am trying to make a letter recall screen as part of a reading span task, using an old version of PsychoPy (v1.83.04). The task shows a sequence of letters (e.g., F, J, H), and then shows a “recall” screen like below.

Participants are asked to recall the letters by clicking on the boxes next to them. Right after each click, the box beside the clicked letter will show a number indicating this is the nth letter they’ve clicked (e.g. if they click on F and then H, the top-left box shows 1 and the top-right box shows 2 following each click).
If they forget a letter they can click on “blank”, and if they want to restart the whole recall they can click on “clear” which makes the numbers disappear.

In other words, when the “blank” or the “clear” button is clicked, these things should happen:

  • the “blank” button should increase the number by 1;
  • the “clear” button should reset the number to 0, and make all the numbers disappear, so that participants can start again.

What did you try to make it work?:
Using Builder, I added the letters (with the checkboxes) and the clear / blank buttons as images.
Then I coded text boxes for showing numbers, which align with the corresponding letter/checkbox images. Thus, when a checkbox is clicked, that text box shows a number.
Some code snippets for reference:

## begin routine:
clickCount = 0 
# text boxes for showing numbers beside each letter
box_F = visual.TextStim(win=win, ori=0, name='box_F',
    text=str(clickCount), font=u'Arial',
    pos=[0.25, 0.90], height=0.1, wrapWidth=None,
    color=u'black', colorSpace='rgb', opacity=1,
# and so on

## Each frame:
if t >= 0.0 and box_F.status == NOT_STARTED and mouse.isPressedIn(letter_F):
        # keep track of start time/frame for later
        clickCount += 1
        box_F.text = str(clickCount)
        box_F.tStart = t  # underestimates by a little under one frame
        box_F.frameNStart = frameN  # exact frame index

if mouse.isPressedIn(blank_button):
        clickCount += 1
if mouse.isPressedIn(clear_button):
        clickCount = 0
        ## No idea how to make the previously-shown numbers disappear

What specifically went wrong when you tried that?:

  • One click on the blank_button adds a lot more than 1 to clickCount. As I read from relevant threads, this seems to be because the code is run at every frame, and one click usually lasts longer than the duration of one frame (so it keeps adding to clickCount). Is there a way to make 1 click only add 1 to clickCount in this old version of PsychoPy?
  • How to make previously-shown numbers inside text boxes disappear, when I click on the “clear” button?

Any suggestions are highly appreciated!


Firstly, you don’t need to create your text stimuli in code. Just use Builder components and refer to those as needed.

Secondly, as you note, mouse button events can span multiple screen refreshes so you need to keep track of not only when a mouse button is pressed, but also when it is released. That way, you can increment the clickCount variable only when .isPressedIn is first detected. The previous posts you mentioned here will show how that is done.

some_text_stimulus.text = ''

Thank you for the rapid response Michael!