1

I am trying to write a piece of code that requires 200 text stimuli to be viewed for 1 second one after the other but I cant get my code to work.

Each stimulus is predefined as a variable:

redkey = visual.textStim(win, "key", (1.0, -1.0, -1.0)
bluekey = visual.textStim(win, "key", (-1.0, -1.0, 1.0)

I have a list 200 items long like so:

x = ['redkey', 'bluekey', 'bluekey', 'redkey'...]

I am trying to write a for loop which cycles through the list and displays each variable for a second (roughly) but I can't get it to work.

win = visual.Window([1024, 768], fullscr = false, allowGUI=false, color = (0.0, 0.0, 0.0))

for item in x:
    item.draw()
    win.flip()
    core.wait(1.0)

When I do this I get the error: AttributeError: 'str' object has no attribute 'draw'.

I tried playing around with vars() and eval() but I can't quite figure out how to do it. Is it even possible to do this?

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
Ron
  • 13
  • 2
  • 1
    Why don't you just make your list contain the actual objects instead of strings (i.e., `[redkey, bluekey, ...]` instead of `['redkey', 'bluekey', ...]`)? – BrenBarn Mar 07 '16 at 20:24
  • Thanks! worked like a charm. – Ron Mar 07 '16 at 20:39
  • 1
    Also, don't create it as a literal list of 200 object references. Use Python to do the heavy lifting: `x = [redkey, blue key] * 100`. Then `shuffle(x)` to randomise it (after `from numpy.random import shuffle`). – Michael MacAskill Mar 07 '16 at 20:42

2 Answers2

2

I'm guessing that you should change:

x = ['redkey', 'bluekey', ...]

to

x = [redkey, bluekey, ...]

As it is, each item in 'x' is a string, rather than the class object that's required.

Boa
  • 2,609
  • 1
  • 23
  • 38
1

More in keeping with PsychoPy you could create your stimulus once and change its colour for each draw

colours = ['red', 'green', 'blue']*200
stim = visual.TextStim(win, text="key")
for entry in colours:
    stim.color = entry
    stim.draw()
    win.flip()
    core.wait(1.0)

That's more efficient than storing many text stimuli in memory

Jon
  • 1,198
  • 7
  • 8