Multiple Variables in Text Component

Hey guys,

I have many instructions for the experiment I am currently working on.
I wanted to present them all in one text component and therefore made a Loop around my text component with the following conditions-file:
conditions

Later I wanted to add more Text for Instr1 and Instr2 but currently I am already struggling with displaying the both texts beneath each other. I tried to do it this way:
TextComponent

unfortunately this does not work. I hope someone has an idea!
I am trying it this way, because I wanted to reduce the amount of text-components from 10 to 1 with a loop.

OS (Win10):
PsychoPy version (e.g. 1.90.1):
**Standard Standalone

Hello. In Builder, there is often a lot of confusion about the the $ symbol represents. It doesn’t mean “this is a variable name”. It means “Interpret what follows as valid Python syntax rather than literal text”. So you can combine variables, operate on them with Python functions, and do pretty much anything you want. For example, one of the simplest way to combine strings of characters in Python is simply to add one to another. In your case:

$Instr1 + '\n' + Instr2

(Notice there is just one $ required, at the beginning of the expression).
Here, \n is a special escape sequence, indicating a new line character.

4 Likes

Hey Micheal,

that perfectly worked for me, thank you!

Is it possible to use different font sizes for the different variables? For example, in Michael’s code, can Instr1 be a different size from Instr2?

Unfortunately no. Text stimuli are currently limited to a single font setting for all their content. To get this effect, you would need to create two separate text stimuli, with different font sizes applied to each as required.

1 Like

Hi Michael,
I have the same problem but I couldn’t solve it the way you suggested. I want to show some values in a routine, so I opened a text box and typed my two variables with the formula $Instr1 + ‘\n’ + Instr2 (with my variable names). If I use this I get a “TypeError: must be str, not int” error and experiment don’t run. If I only use one variable, the experiment runs but instead of showing the values of the variable, it shows the variable name without the $ icon.
I have the variable and the data in the condition file that I put in the big loop. I’m not sure what I’m doing wrong.
Also, I wonder if it’s possible to use both text and variables in the same text stimuli. So if I want to present the same text with different values in each trial, can I do something like this: Example sentence $variable example…
If I write this it gives a syntax error, so is there a way to combine these two?

Thank you!

Hi, the error suggest that one of your variables is interpreted as an integer. Putting str() around this variable will likely fix that. For your second question, you can simply say $“example sentence“ + variable + „end of example.“.

2 Likes

It worked! Thank you very much!