Search code examples
pythonpsychopy

Psychopy: How to display Japanese?


I am want to display some Japanese sentences on Pyschopy through coding and I used code like this:

from Psychopy import visual,core

win=visual.Window([400,400])

message=visual.TextStim(win,text='先生を呼んだ学生')

message=setAutoDraw(True)

win.flip()

and the response is that "SyntaxError: Non-ASCII character '\xe5' in file D:\用户目录\我的文档\untitled.py on line 5, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details".

I guess it's illegal to use TextStim to present Japanese, but I see in the builder view it's ok to do so. So I wonder if there's some way for me to display Japanese by coding?


Solution

  • PsychoPy is going to add beta support for Python 3 shortly, which will improve unicode handling, but for the timebeing, you should be able to run this even under the current Python 2.7. This tweak to your code works for me:

    #!/usr/bin/env python2
    # -*- coding: utf-8 -*-
    
    from psychopy import visual,core
    
    win=visual.Window([400,400])
    
    message = visual.TextStim(win, text = u'先生を呼んだ学生')
    
    message.draw()
    
    win.flip()
    
    core.wait(5)
    

    i.e. explicitly declare the utf-8 encoding in the header but also precede your string literal with u to indicate that it is a unicode string (this step eventually won't be necessary under Python 3 I think).