如何打印列表中不同颜色的单词,然后将其显示在 python 中的 window 上?

how to print words in a different color from a list and then show it on a window in python?

如何给 python 中的列表中的每个单词添加颜色,然后将其打印到 window 中?

我的代码是这样的:

cognitivo = ["Plan","Organize","Help","liquidate","solvency","Prioritize","work","business","achievements","Control"]
for stimulus in cognitivo:
    mens = visual.TextStim(win, text=stimulus)
    mens.draw()
    win.flip()
    #core.wait(1.0) 

只需复制列表,因为它是用 for 循环打印的,而不是复制我所做的所有代码...只打印一种颜色,而我想要的是每个单词都有一种特定的颜色...拜托,有人可以帮助我吗?谢谢

为了让事情变得更简单,您可能想要为每个试验创建一个字典,将所有与试验相关的信息一起保存在一个对象中(例如试验的文本和颜色)。这使得循环遍历此类词典列表并轻松访问试用值变得非常容易。查看 PsychoPy TrialHandler class,它将完成所有这些以及更多(包括保存数据)。

但为了适合您的简单代码片段,试试这个:

cognitivo = ['Plan', 'Organize', 'Help', 'liquidate', 'solvency', 'Prioritize', 'work', 'business', 'achievements', 'Control']
# create some corresponding colours:
colors = ['red', 'green', 'yellow', 'blue', 'black'] * 2

# initialise the text stimulus just once:
mens = visual.TextStim(win, text = 'XXXXXX')

# loop through the stimuli:
for stimulus in cognitivo:
    # update the stimulus:
    mens.text = stimulus
    mens.color = colors.pop()

    # display for 1 second at 60 Hz:
    for frame in range(60)
        mens.draw()
        win.flip()

请注意,一般来说,您不应该一直重新创建刺激。一般只需初始化一次,然后更新其属性即可。创建刺激通常比更新现有刺激需要更长的时间。