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
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>
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:
@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).
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!)
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?