Change blackgound color based on the sequence position condition

OS (e.g. Win10):
PsychoPy version (e.g. v2020.2.10):


What I trying to achieve:
I want to create an experiment which shows sequences of images with the background color changes accordingly. Briefly, I have six block conditions, which represent the order of image display and background color criteria. Within a block (after a random block condition is chosen), the order of the images and background color is defined by the block condition, eg. if repeat1.csv in the block condition is selected, the order is: neutral images with white background(event_1)->emotional images with black background (event_2). Note that neutral images is not always associated with white background, nor does emotional images. There are four images shown in a given event. The images are chosen from either the neutral or emotional image pool based on it’s event position. The scheme below may help:

I have successfully built a demo, which shows sequences of images. But I have don’t have any idea how to change the background color based on the sequence position.

What I have so far is below:
With the outer “sixblocks” loop, the “eventcond_1” loops gets which block condition is being selected and opens the .csv file that contain the order of images in each events. Then the “Imageselect_1” loop open either “emotion.csv” or “neutral.csv” files based on the event_1 condition. The “emotion.csv” and “neutral.csv” contains the list of names of the images.

This is my blockcondition file:

If there is a way to indicate the background condition with each event, that would be great. Since Imageselect_1 loop only choose the condition with Event1 (see below). I don’t know where to put the background color condition. Nor do I know how to customize the background color.

Thanks in advance!

You could change the background color by changing win.color, but the docs says it easier to draw a rectangle that fills the screen with your color of choice.

I had some trouble understanding your experiment, and the nested loops. But you should be able to use code components to write custom code to choose color based on some condition.

Reading the docs again, it does seem like using a rectangle is necessary only if you need to change the color “on the fly”.
win.color seems to work fine in this test code I wrote - which changes to a random background color on each press of spacebar:

## Begin experiment
win.colorSpace = 'rgb255'

## Each frame
if 'space' in defaultKeyboard.getKeys():
    win.color = randint(256, size=3)```

Hello Filiabel,

Thank you so much for the quick response. I have tried the create a polygon stimuli instead of win.color, it is much easier. However, as I created the polygon as big as the screen size, it covers the other image. Is there a way to show both the big polygon and images with images on top of the polygon?

If not I will try create several polygon to cover the empty space.


Make sure that your polygon component is placed above the other components in Builder view. Right-click the name of the polygon component and choose Move to top. I believe this should work.

Thank you! I got it in the opposite direction (in the bottom). A million thanks!

1 Like