Multi colored strings

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): WIN 10
PsychoPy version (e.g. 1.84.x): 2023.2.3
Standard Standalone? (y/n) If not then what?: pip install
What are you trying to achieve?:

Is there a way to achive the following (see pseudo-code below)?


string1 = “my_multi_”

string2 = “colored_string”

string1.set_color = RED

string2.set_color = BLUE

string1 + string2


Many thanks for any answers. Ideally this should also work online


Hi @Henning_Holle,

As far as I know, it is not possible to have a single Text (or Textbox) component that is multi-coloured.
There are two alternate options that might work.

Option 1: Images

Instead of using text components, you could pre-make your multicoloured text in a graphic editing software (such as Powerpoint or Inkscape) and save those as images. This would give you maximum control over the colour combinations and string formatting. You would then use Image components in PsychoPy to display your multicoloured strings.

This should work both online and offline, but you would need to take care that the image is sized correctly in order to display the text properly.

Option 2: Multiple text components

You could have 1 text component for each colour/fragment. This should be more efficient than many images, but depending on the complexity of your multi-coloured text it might not be feasible. (getting two components to line up and look like one string would be pretty easy, but more components would be difficult).

This should work both online and offline, but you would need to take care the multiple components are aligned correctly.

Hope that helps,

1 Like

For option 2 it might be possible to use a left anchor for the right text and a right anchor for the left text. This can work online but you’d need to check my crib sheet or elsewhere for the correct code translation.

1 Like

many thanks for your quick reply. I should have provided a bit more context. The words that I want to colour will be embedded in a longer multi-sentence context. So using multiple text components will be difficult. Is it possible to embed code that highlights just specific words in a sentence context? (see pseudo-code below)

If you use a fixed width font then it’s possible to calculate where any given word should be based on the length of the string and spaces, and then position a second text component accordingly.

1 Like