Have a textbox appear conditionally

OS (e.g. Win11):
PsychoPy version (e.g. 2024.2.1post4):
Standard Standalone? (y/n) If not then what?:
What are you trying to achieve?:
On each trial, participants will be presented with an image. If the participant responds ‘a’, ‘s’, or ‘d’, I would like for an editable textbox to appear below the image. In order to proceed to the next trial, the participant must press ‘enter’. If the participant responds ‘j’, ‘k’, or ‘l’, I don’t want the textbox to appear and I would like the routine to automatically proceed to the next trial after the participant has made their response.

Additionally, I’m not sure if this would make a difference, but would having the loop type set to “random” or “sequential” make a difference in executing the above script? In the sample file I have attached, I set the loop as “sequential” because I want the images during practice to be shown in the order they appear in the excel file. However, in the main task I would like to present the images in a random order.

What did you try to make it work?:
I’m very new to PsychoPy, so over the past few days I’ve tried a hodgepodge of various bits of code from posts regarding conditional textboxes on this forum page as well as some suggestions from chatGPT. With my latest attempt, I inserted a code component with the following script:

In the Begin Routine tab:
if resp.keys == [‘a’, ‘s’, ‘d’]:
textbox.setAutoDraw(True)

else:
textbox.setAutoDraw(False)

In the End Routine tab:
if resp.keys in [‘a’, ‘s’, ‘d’] and ‘enter’ in resp.keys:
continueRoutine = False

On one attempt, I tried incorporating advice from this post (Editable TextBox: Continues to appear on subsequent routine (doesn't end in current Routine)) about always having the textbox visible, but its position changes (so if a,s,d [0,-0.35] (visible) or j,k,l [0,2] (not visible) but I wasn’t sure how to change the position of the textbox in a conditional way (e.g. if a, then move to [0,-0.35]). And I also run into the problem of the program moving to the next trial without hitting enter (this is described in more detail below).

What specifically went wrong when you tried that?:
Every time I’ve attempted to setAutoDraw(false), the textbox always appears and remains on screen.

Additionally, I noticed that if a participant wanted to type one of the response keys (i.e., ‘a’, ‘s’, ‘d’, ‘j’, ‘k’, ‘l’), the routine proceeds to the next trial. I don’t know what to do to stop the program from proceeding to the next trial prior to a participant typing their response and hitting ‘enter’.

Include pasted full error message if possible. “That didn’t work” is not enough information.
I can generally get the task to launch and run through. It just doesn’t do what I want it to do.

I have also uploaded all the files I’ve been using to set up this task.

practice.psyexp (27.1 KB)
Task2_Practice.xlsx (8.9 KB)




I haven’t tested it, but here is a psyexp file with my recommended solution.

I’ve duplicated your Practice routine and edited it so that the first copy doesn’t show the textbox and the second one does. Then I’ve used the code component and routine settings to skip the second routine if j, k or l is pressed.

The second routine ends on return (not enter).
practice.psyexp (31.5 KB)

Hello,

Thank you for your advice! The task is now working perfectly!