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

被刻印的时光 ゝ 提交于 2019-12-13 00:04:11

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/46275119/psychopyis-it-possible-to-display-2-words-in-different-colors-within-the-same-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!