Displaying unicode text strings in Psychopy 3

Dear users

I am building a simple experiment in which a series of words are presented onscreen for 3 seconds each. I am implementing this in Builder, using a Text object to display the words.

I need the second letter of each word to be underlined. Note that I plan to use a list of words read from an excel sheet, but for the purposes of debugging I’m ignoring the excel sheet and just trying to present the word “hello”.

So far I’ve taken a unicode-based approach, for example:

print(‘h\u0332ello’)

This works in PsychoPy’s Coder View: see screenshot below

Capture

However when I use this code in the builder it fails. I have tried pasting print('h\u0332ello') directly into the Text field of the Text object, which results in PsychoPy literally displaying “h\u0332ello” onscreen. I have also tried entering $word into the text field, and then defining word as word = print('h\u0332ello') in a code snippet. This latter approach causes the following error:

UnicodeEncodeError: 'charmap' codec can't encode character '\u0332' in position 3: character maps to <undefined>

Can anybody recommend a way to get this working?

$'h\u0332ello'

although for me that underlines the h rather than the e, but I don’t know enough about unicode to know if that is correct or not.

$'he\u0332llo'

underlines the e for me.

Using print() in any way in Builder components is not going to work. print() is a Python command that sends output to the console. It can’t do anything meaningful to stimuli displayed in a PsychoPy window.

All assuming that you are using the Python 3 version of PsychoPy - Python 3 deals natively with unicode strings, unlike Python 2.

If you want to programatically underline the second letter using a variable from a conditions file (called, say word), you could put this in your text field:

 $word[0:2] + '\u0332' + word[2:]

@Michael, thanks for the reply! I tried implementing your suggestions (both $'he\u0332llo' and $word[0:2] + '\u0332' + word[2:]), both cause the experiment to crash and give the following error:

TypeError: ord() expected a character, but string of length 2 found

EDIT:
Some additional info: yes, I am using the Python 3 version (specifically, PsychoPy version 3.2.4). Running on Windows 10.

It works for me, so this is a bit puzzling. I think you would need to post a minimal example so that the issue can be replicated (i.e. a .psyexp file showing nothing but this issue).

Hi Michael,

Here is a simplified version of the experiment that has a single routine, containing only a text object with $'h\u0332ello’ in the Text field. This produces the problem described above (on my computer, at least!)

pilot_2.psyexp (4.1 KB)

It works fine for me. Just to be absolutely sure, can you switch to the Coder view and type this in the Shell tab, and let us know the output?

import sys
sys.version

How puzzling. Here is the output you requested:

‘3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)]’

OK so I’m a bit lost then. I am using a Mac but wouldn’t think that this should be an operating system- related thing. But can’t suggest anything else at this stage. Hopefully someone else has something to contribute?

How about printing out the system encoding in both Builder and Coder, it may shed some light on what is happening:

import sys
print(sys.getdefaultencoding)