| Reference | Downloads | Github

Only display text under certain conditions


I am trying to have some text on the screen over an image that is being displayed, but only under certain conditions–if the participant has not pressed any keys yet, and if they are on the first three loop iterations.

I have tried a code component that says this

if len(imageSwitcher_2.keys) != 0 or trials_3.thisN > 3:
text_13.opacity = 0
elif len(imageSwitcher_2.keys) == 0 and trials_3.thisN <= 3:
text_13.opacity = 1

imageSwitcher_2 is the keyboard component
trials_3 is the loop
text_13 is the text

I’ve also tried putting
trials_3.thisN <= 3 & len(imageSwitcher_2.keys) == 0
in the opacity field of the text component

Neither solution has worked.

Any suggestions?

Could you use the ‘preformatted text’ option for the code in your post? That makes the code easier to read. The way you have formatted your text, it is impossible to see what code is effected by your if and elif statements.

If your sollutions haven’t worked, what did happen instead? Was there an error, was the text visible throughout or was it invisible?

Hi Ashish,
a solution that works for me is to find the line where the text is set and wrap it in your condition like this:

if len(imageSwitcher_2.keys) == 0 and trials_3.thisN <= 3:
    text_13.setText(u'here your text is set', log=False)
    text_13.setText(u'', log=False)

Note, that I’ve set the content of the text to be changing with each frame in the builder. Is it important to have it invisible, or is it okay to rather un-set it?


My mistake LukasPsy. For future I will absolutely do that.

I solved the problem moving the position of the text_13 component in the builder view. Then I dealt with the key presses by putting
len(imageSwitcher_2.keys) == 0
in the opacity field of the text component in builder view.

To deal with the trial number I put

if trials_3.thisN > 2:
    text_13.setText(u'', log = False)

in a code component Each Frame as per your suggestion schubisu

Thanks guys!