psychopy.org | Reference | Downloads | Github

Multiple variables in Text component defined by user

Hi!

I am trying to include several variables (a1 and a2) in my Text component. Each of these variables (numbers) is defined by user. So I have a code component which helps me to visualize the input.
Here is part of my code for a1 variable:

keys = event.getKeys()
if len(keys):
    if 'space' in keys:
        a1.text = a1.text + ' '
    elif 'backspace' in keys:
        a1.text = a1.text[:-1]
    elif 'lshift' in keys or 'rshift' in keys:
        modify = True
    else:
        if modify:
            a1.text = a1.text + keys[0].upper()
            modify = False
        else:
            a1.text = a1.text + keys[0]

And I have a text component with my 2 variables:

$a1 + '\n' + a2

I get the following error:

a1.text = ''
AttributeError: 'str' object has no attribute 'text'

How should I define my a1 and a2 variables?
Thanks!

@MarinaIosifian, this error occurs because you are tring to access a text attribute of a Python string. There is not enough information here to see what is happening, but it looks like you are following the textInput demo from Pavlovia. If so, then you need to separate your a1 a2 storage variables from your text components, that is, a1 should be treated as a temp variable to feed text to your text component, and not used to set the text component directly in the code component. See the code in the textInput demo.

Thanks a lot for your response! Yes, exactly, I was following the demo you cited. But there is only one text input in the demo, unfortunately for me.
Does it mean that I should create two text compontents, a1 and a2? When I tried this appraoch, I could only insert text in one text component, while the other was not accessible.

I think you should create text components that have a different name from a1 and a2, then you should avoid this error that you are currently receiving. You get this error because strings do not have text attributes, that is something specific to PsychoPy text components, which are the objects that draw the text on screen. In the textinput demo, you have textAdd as the temp variable, for collecting text from the keyboard, which feeds into the text component called "text" e.g., text.text = textAdd.

Thanks a lot! In the demo, the variable is called ‘text’. The text component name is ‘text’ as well. I called my text components ‘a1’ and ‘a2’ , left the Text inside empty (like in demo), and added the code similar to the one in the demo. In this code I added lines for my second component (e.g., a1.text = textAdd and a2.text = textAdd). I do not recieve any error now, but when I type something the input i visible only for the first variable in my code (e.g., a1).

Sorry, in the demo the variable is called textAdd, and the text component is called text, which also has an attribute also called text, which is the actual text for the text component.

I am not sure what you are trying to achieve, do you want text to appear in two text components simultaneously?

I try to have several text inputs on one screen. So my subjects can type different numbers in different locations of the screen. Something like that :

image

Thanks for not giving up!

I see, well I have a demo of something similar, take a look and adapt it to your needs. Each box that gets clicked becomes the active text input - try it here (note, I only allowed number keys and backspace - see keyList in code): textInp [PsychoPy]

Here is the Builder file with the code.

textInp.psyexp (15.5 KB)

1 Like

That is fantastic, thank you so much!