Displaying a variable's value in a text field leads to syntax error

714 views
Skip to first unread message

Tudor

unread,
Nov 26, 2015, 11:51:26 AM11/26/15
to psychopy-users


This is probably a silly one but am at my wit's end having tried all I could think of. I am trying to display the value of a variable defined in a code component in a different component (text) of the same routine.

The code component has the following in BeginExperiment::
aaa = 2

And the text component contains:
$aaa
Welcome!
(..expt instructions)

This gives a syntax error, unlesss I take everything out of the field and only leave $aaa.

!!!

Tudor Popescu

unread,
Nov 26, 2015, 11:59:53 AM11/26/15
to psychop...@googlegroups.com
Forgot to mention that the text component was normally set to Every Repeat, and that the variable's value is displayed only if I take out the rest of the instructions and set the field to Constant. If left at Every Repeat, isntead of the variable's value, what is displayed is a portion of the raw code of the script!

Any thoughts? Thanks!

--
You received this message because you are subscribed to a topic in the Google Groups "psychopy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psychopy-users/ZqdBGP1obSk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/3944658d-9738-4f30-8e7e-184f0314394e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeremy Gray

unread,
Nov 26, 2015, 12:01:22 PM11/26/15
to psychop...@googlegroups.com
The presence of $ turns the contents of the whole text field into something that will be regarded as python code, instead of being interpreted as a string.

So its essential to understand python syntax to know what to expect, and how to arrange for it to do what you want.

$str(aaa) + """
Welcome!
(..expt instructions)
"""

--Jeremy


--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.

Tudor

unread,
Nov 27, 2015, 5:59:12 AM11/27/15
to psychopy-users
Thanks Jeremy, that worked.

However, I have to say it was actually a less "silly" one than I anticipated in my original post. Not sure how users not already familiar with Python are to guess such a workaround. I had as a guidance the documentation for using variables in text components, which says:

To indicate to PsychoPy that the value represents a variable or python code, rather than literal text, it should be preceded by a $. For example, inserting intensity into the text field of the Text Component will cause that word literally to be presented, whereas $intensity will cause python to search for the variable called intensity in the script.

I'd have added a paragraph about the current issue but apparently documentation is not set up as wiki. If you think it might be helpful to future users who bump into the same problem it might be good to make a note there about cases where text fields contain instructions in addition to variable names.

Once again, thanks for your kind and prompt help.

Jeremy Gray

unread,
Nov 27, 2015, 10:35:10 AM11/27/15
to psychop...@googlegroups.com

Thanks Jeremy, that worked.

Great
 
However, I have to say it was actually a less "silly" one than I anticipated in my original post. Not sure how users not already familiar with Python are to guess such a workaround.

Its not a work-around, actually. But your experience is proof enough that the documentation does need more work. 
 
I had as a guidance the documentation for using variables in text components, which says:

To indicate to PsychoPy that the value represents a variable or python code, rather than literal text, it should be preceded by a $. For example, inserting intensity into the text field of the Text Component will cause that word literally to be presented, whereas $intensity will cause python to search for the variable called intensity in the script.

I'd have added a paragraph about the current issue but apparently documentation is not set up as wiki.

Cool. We tried that for a while but almost no one contributed. If you write up a paragraph, I'll add it. 

Or you can add it yourself (make a fork of psychopy, edit the appropriate .rst file(s) in psychopy/docs/source, and submit a pull request.
 
If you think it might be helpful to future users who bump into the same problem it might be good to make a note there about cases where text fields contain instructions in addition to variable names.

I agree that multiple examples would be good here, including both simpler ones (like the intensity one), and more complex ones too.

--Jeremy

 
Reply all
Reply to author
Forward
0 new messages