| Reference | Downloads | Github

Can't call text position and allowed keys as variables

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): v2021.1.2
Standard Standalone? (y/n) If not then what?: yes
What are you trying to achieve?:

  • create a variable before experiment that defines text position and call it later for varies stimuli creation.
  • create a variable in my loop excel that defines allowed keys per trial and call it in the stimuli input box

What did you try to make it work?:

  • I tried coding component (but it gives error in terms of indexing problem)
txt_position = [0,0.2]
#in txt component of the position box
position$: txt_position
  • I tried adding a column of response in the Excel with data saved as: [‘space’,‘1’]

What specifically went wrong when you tried that?:

  • Text component: it will fail to create the stimuli and stop the experiment from running. It seems like it can’t be predefined?
  • Keyboard component: set repeat doesn’t register the variable calling. It doesn’t seem to find/read/extract the information.

I think there are other ways round to make my experiment runs, but loving these two problem can really improve my development efficiency. I also read through couple posts related to these, none of the solution has worked so far.

Any advise is appreciated.


Can you post the errors you get from the Stdout in Runner?

For text creation:

“C:\Users\fuq01\AppData\Local\PsychoPy3\lib\site-packages\psychopy\visual\”, line 806, in draw
GL.glTranslatef(self.posPix[0], self.posPix[1], 0)
IndexError: index 1 is out of bounds for axis 0 with size 1

For allowed keys:

Traceback (most recent call last):
File “C:\Users\fuq01\AppData\Local\PsychoPy3\lib\site-packages\psychopy\experiment\components\”, line 238, in writeFrameCode
keyList = eval(allowedKeys)
File “”, line 1, in
NameError: name ‘response’ is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\fuq01\AppData\Local\PsychoPy3\lib\site-packages\psychopy\app\builder\”, line 1165, in runFile
File “C:\Users\fuq01\AppData\Local\PsychoPy3\lib\site-packages\psychopy\app\runner\”, line 635, in runLocal
File “C:\Users\fuq01\AppData\Local\PsychoPy3\lib\site-packages\psychopy\scripts\”, line 73, in generateScript
compileScript(infile=exp, version=None, outfile=filename)
File “C:\Users\fuq01\AppData\Local\PsychoPy3\lib\site-packages\psychopy\scripts\”, line 242, in compileScript
_makeTarget(thisExp, outfile, targetOutput)
File “C:\Users\fuq01\AppData\Local\PsychoPy3\lib\site-packages\psychopy\scripts\”, line 221, in makeTarget
script = thisExp.writeScript(outfile, target=targetOutput)
File “C:\Users\fuq01\AppData\Local\PsychoPy3\lib\site-packages\psychopy\”, line 212, in writeScript
File “C:\Users\fuq01\AppData\Local\PsychoPy3\lib\site-packages\psychopy\experiment\”, line 217, in writeBody
File “C:\Users\fuq01\AppData\Local\PsychoPy3\lib\site-packages\psychopy\experiment\”, line 206, in writeMainCode
File "C:\Users\fuq01\AppData\Local\PsychoPy3\lib\site-packages\psychopy\experiment\components\keyboard_init
.py", line 241, in writeFrameCode
self.params[“name”], “Allowed keys list is invalid.”)
psychopy.experiment.utils.CodeGenerationException: choice_key_2:

This is what I have in the Excel:

Thank you for your help.Best,

Few suggestions:

  • Is code component placed above the text component? Maybe position is not set at first draw of stim.
  • Use a print statement and print(txt_position, txt_position[0]) to confirm it is what we think it is.
  • Try splitting it up in two variables and make the tuple in pos field.
posX, posY = 0, 0.2

position$: (posX, posY)

Your keyboard error seems to have been fixed in v.2021.1.4. See discussion here. Try installing the newest version.

1 Like

Dear Filip,

Thank you so much! Splitting and tuple suggestion solved the txt location problem, and update to 2021.1.4 solved allowed key issues.

:smiley: Many thanks!


1 Like