| Reference | Downloads | Github

Image switching task - set duration of second image to remainder of duration of first

Hello everyone,
PsychoPy newbie here. I have a task where in one trial, participants have the option (but not the requirement) of switching an image to a different image. No matter what they choose to do, they will look at something for a total of 6 seconds. So if they don’t press spacebar they view image #1 for 6 seconds. If they press spacebar after 2 seconds, they will spend those previous 2 seconds looking at image #1 and the next 4 seconds viewing image #2.

My question is: How do I set the duration of image #2 to be the remainder of their 6 second viewing period?

Hi Ashish,


  • a keyboard component with duration 6 seconds, NOT set to force end of the routine.
  • two image stimuli, one for image #1 and one for image #2. Set both to have a duration of 6 seconds.

For image 1, put this in the Opacity field:

len(your_keyboard_component_name.keys) == 0

and this for image 2:

len(your_keyboard_component_name.keys) > 0

and set each opacity field to update “every frame”.

i.e. on every screen refresh, this checks to see if a key has been pressed (i.e. if the length of the stored list of keys is > 0) . If no key has been pressed, the opacity of the first image is 1 (as that first expression evaluates to True) and the second image is 0 (as the second expression evaluates to False). The opposite is the case when a key has been pressed.

This handles the timing for you automatically.

Beautiful. Thank you!