Heaven
Heaven

Reputation: 23

Psychopy:Is it possible to display 2 words in different colors within the same frame?

I want to display a sentence with words in different colors within the same frame . But all the code I've seen just change the color of the stimuli as a whole , not a part of it ... Here's my code for a try ,but it failed

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from psychopy import visual,core 
win = visual.Window([400,400])
sent=[u'先生',u'を呼んだ',u'学生が',u'教室に',u'入った。']
sent[0].color=[1,1,1]
sent[1].color=[1.0,-1,-1]
sentence=visual.TextStim(win,text=sent[0]+sent[1])
sentence.setAutoDraw(True)
win.flip() 

I am wondering whether there's a way for me to change the text color before it becomes a visual.TextStim ?

Upvotes: 1

Views: 688

Answers (1)

Michael MacAskill
Michael MacAskill

Reputation: 2421

No, the TextStim applies formatting (colour, italic, etc) to its entire contents. If you want words with different colours, they unfortunately each need to be in their own TextStim

An alternative is to the use TextBox class which I think allows per-character formatting, but only for monospace fonts: http://www.psychopy.org/api/visual/textbox.html#psychopy.visual.TextBox

Having said that, I've found it doesn't work reliably at present, at least on Mac OS.

Upvotes: 1

Related Questions