| Reference | Downloads | Github

Opacity of text stimuli is not updating

OS (Win10):
PsychoPy version (3.1.5):
Standard Standalone? (y):

What are you trying to achieve?:
I am rather new to psychopy and python in general.
I am trying to adjust the opacity of text stimuli in one routine based on clicked responses that occur in a subsequent routine (in the same loop). I have valid clickable text stimuli, and when one of them is clicked in the second routine, when the loop repeats, I want the clicked stimuli to update the opacity of the corresponding text stimuli in the first routine.

What did you try to make it work?:
I have the opacities of the three text stimuli set to the variables “Step2_Opacity”, “Step3_Opacity”, and “Step4_Opacity”.

In the begin experiment tab, I set each of those variables equal to 0.

Step2_Opacity = 0
Step3_Opacity = 0
Step4_Opacity = 0

In the begin routine tab, I set each of those variables equal to themselves. In the text_stim window under advanced, I have opacity to update every repeat.

Step2_Opacity = Step2_Opacity
Step3_Opacity = Step3_Opacity
Step4_Opacity = Step4_Opacity

In the subsequent routine, in the each frame tab, I have

if InfoSearchSelection_Click.isPressedIn(Step2Dummy_Text):
    Step2_Opacity = 1

if InfoSearchSelection_Click.isPressedIn(Step3Dummy_Text):
    Step3_Opacity = 1

if InfoSearchSelection_Click.isPressedIn(Step4Dummy_Text):
    Step4_Opacity = 1

What specifically went wrong when you tried that?:
The experiment runs fine, but the opacity of the three text stimuli is still stuck at 0. The updated value of 1 is not carrying over when the loop repeats. I also have the click event set to end the routine on a valid click, and when I run through it, it is appropriately ending the routine when I click one of the text stimuli and staying in the routine when I click elsewhere.

This is unnecessary - that code can be deleted. A variable is always equal to itself, and hence this code has no effect. The values of these variables are updated in the second routine, as required.

Do you know this to be the case? Try inserting this temporary debugging code in the “begin routine” tab:


This will tell you if the problem is with the variable changing correctly or not. If the variable is updating, but is having no visible effect, the most likely cause is that you have not set the “opacity” field to update on “every repeat”. If left at the default value of “constant”, the changing variable will have no effect. But you have said that this is set to update.

If the variable is not changing, then you have isolated the problem to the code in the second routine. Again, insert some (temporary) debugging code like this:

if InfoSearchSelection_Click.isPressedIn(Step2Dummy_Text):
    Step2_Opacity = 1
    print('Step2 clicked.')

etc, to verify that the clicks are being detected.

Hi Michael,
Thank you for your reply! So I added in those two lines of debugging code and the first time the routine runs, I am getting an opacity of 0 for Step2_Opacity (which is to be expected), and when the second routine in the loop runs, I am getting a print of ‘Step2 clicked’. In the next repeat, I am getting an opacity of 1, but that change is not reflected in the actual opacity of the text stimulus.

So need to check:

  • that the opacity field is set to update “Every repeat”, and
  • the code component is above the text stimulus, so that the latter gets to refer to the current value of the Step2_Opacity variable.

Hi Michael,

Yes, the opacity field is set to every repeat and the code component is above all of the text components.

Oh wow, you’ve hit upon an old bug/limitation that I’d forgotten about:

i.e. controlling opacity for text stimuli doesn’t work like it does for other stimuli, unless the text content is also being changed at the same time.

A work-around is suggested in the post below, where you can get the same effect indirectly, by having another stimulus (e.g. a filled polygon that matches the background colour of your window) overlap each text stimulus. When you want the text stimulus to be invisible, set the opacity of that polygon to be 1, and set it to 0 when you want the text to be seen:

Although this problem really does need to be fixed in PsychoPy itself, hopefully this will get you up and running.

1 Like

Hi Michael,

Wow! I thought I was losing my mind. Glad to know my head was at least in the right place. Thanks for all your help and for pointing me in the right direction. Cheers!

1 Like

Good luck. For future reference for others, if this work-around isn’t viable (e.g. because the text stimulus is being drawn on top of a complex image background and can’t simply be masked by a rectangle), then alternatives to make it appear invisible are

  • to set its position to coordinates that are just outside the visible bounds of window.
  • to set its text content to be be empty (less preferred, as this can be slow).
1 Like