psychopy.org | Reference | Downloads | Github

Error message --> AttributeError: 'list' object has no attribute 'draw'

Hey,

I want to program an RSVP experiment.
Within a trial I want to display 15 stimuli, of which 13 Non-Words and 2 Words (targets).
Each 100ms I want to display a new stimuli in the window. The Words (targets) will always be 300ms separated from each other, so 3 non words in between them.

The code fails at showing the stimuli on my screen.
I get the message:

    NonWords[0:4].draw()
    AttributeError: 'list' object has no attribute 'draw'

I have looked at multiple pages of how I can make this work but I do not find the right solution. So any suggestion that will make this work in the script is welcome.

This is the current code I use when I get the error-message.
The words and non-words I use come from an excel file. that I uploaded with “infile()”.

    # display the first stimuli
    NonWords[0:4].draw()
    win.flip()
    core.wait(0.1)
    
    # draw the stimulus T1
    EmoWords[0].draw()
    win.flip()
    core.wait(0.1)
    
    #display distractors
    NonWords[5:7].draw()
    win.flip()
    core.wait(0.1)
    
    #display T2
    EmoWords[1].draw()
    win.flip()
    core.wait(0.1)
    
    #display end stimuli
    NonWords[8:12].draw()
    win.flip()
    core.wait(0.1)

I have also included the full script and the excel file I use.
word-list.xlsx (9.2 KB) AB Cogantes.py (7.1 KB)

Thanks in advance!
Laurence

When you get errors like this, it is because the object is not of the type that you expect it to be. You can debug things this way with some (TEMPORARY) print statements:

print(some_object)
print(type(some_object))

In the absence of that information (and you really should post the entire text of the error message, it narrows things down for us a lot), I would guess the error occurs on lines like this:

NonWords[0:4].draw()

The error message says exactly what the issue is: NonWords[0:4] will be a list of stimuli rather than a single stimulus. Lists are generic Python objects and don’t have a .draw() method, which is specific to PsychoPy stimulus objects. To draw a list of items, you would need to iterate over the individual items, e.g.

for stimulus in NonWords[0:4]:
    stimulus.draw()

In other lines, you are extracting individual stimuli from a list and so expressions like this should work fine:

EmoWords[0].draw()
1 Like

Hey Michael

thanks for the comments. I think my post is now following the rules.

I tried to use the for-statement so as you said. Now the problem is that my stimuli are strings and these also doesn’t allow me to draw these with the function .draw().

You need to draw text components rather than strings.

Yes, before attempting to do any drawing, first cycle through all of your strings and turn them into PsychoPy TextStim objects, which do know how to draw themselves into a window. e.g.

for nonWord in NonWords:
    nonWord = visual.TextStim(your_window_name, text = nonWord)

Creating lots of objects like this can be time consuming, so do it at a non-time critical point (e.g. before your trials start).
https://www.psychopy.org/api/visual/textstim.html

If these concepts aren’t yet clear, I would highly recommend that you work your way through some of the example Python scripts available from the “demos” menu in the Coder view. Or alternatively, consider using the Builder graphical view to do most of the heavy lifting for you, so you only need to enter the occasional snippet of custom code. This is particularly because while you are just learning the PsychoPy API, there is another whole layer of understanding needed, which is about performance considerations. Builder solves a lot of that for you by writing performance-aware code for you.