How do I use TextBox2 in a python script?

Hi,

I have written my experiment by hand in python using the psychopy library. The reason I am using a script rather than the builder is that I need very specific control over the trials that I have programmed and how their data is handled.

All of that works fine, I just wanted to add a simple text box at the very end of the experiment so that the participant can type out an answer which can then be saved into a list for example.

I could not find anything on how to use TextBox2 in code online other than the documentation. However, I could really do with a simple example to see how it is implemented.

If someone could show me how to implement this that would be great, thank you!

For the most part TextBox2 works the same as any other stimulus - you create it using the values described in the documentation and then just make sure to call .draw() each frame or set .autoDraw to be True.

Making an editable TextBox2 specifically is also relatively straightforward - when you create it, just make sure that editable=True. This means that the participant can type responses and see them appear in the box, just like a HTML textarea or any other text control. The current contents of the textbox are accessible via .text, so you can use that to get the participant’s response when they’re done.

Thank you! Managed to get it to work :slight_smile:

If anyone comes across this in the future, here’s how I implemented it:

textbox = visual.TextBox2(win, ‘’, editable = True)

while ‘=’ not in textbox.text:
instructions.draw()
textbox.draw()
win.flip()

print(textbox.text[:-1])

Used the ‘=’ key for submission and did not include it in the output I printed

1 Like