# Only display text under certain conditions

**URL:** https://discourse.psychopy.org/t/only-display-text-under-certain-conditions/3301
**Category:** Builder
**Created:** [October 24, 2017, 5:03am UTC](https://discourse.psychopy.org/t/only-display-text-under-certain-conditions/3301 "2017-10-24T05:03:27Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ashish](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@Ashish](https://discourse.psychopy.org/u/Ashish)
#### Post date: [October 24, 2017, 5:03am UTC](https://discourse.psychopy.org/t/only-display-text-under-certain-conditions/3301/1 "2017-10-24T05:03:27Z")

</div>

Hi,

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  
text\_13.draw()  
win.flip()  
elif len(imageSwitcher\_2.keys) == 0 and trials\_3.thisN \<= 3:  
text\_13.opacity = 1  
text\_13.draw()  
win.flip()

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?

---

<div class="post-metadata">

### Author: ![LukasPsy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lukaspsy/32/4582_2.png) [@LukasPsy](https://discourse.psychopy.org/u/LukasPsy)
#### Post date: [October 24, 2017, 9:50am UTC](https://discourse.psychopy.org/t/only-display-text-under-certain-conditions/3301/2 "2017-10-24T09:50:49Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![schubisu](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/schubisu/32/1686_2.png) [@schubisu](https://discourse.psychopy.org/u/schubisu)
#### Post date: [October 24, 2017, 3:14pm UTC](https://discourse.psychopy.org/t/only-display-text-under-certain-conditions/3301/3 "2017-10-24T15:14:16Z")

</div>

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:

```auto
if len(imageSwitcher_2.keys) == 0 and trials_3.thisN <= 3:
    text_13.setText(u'here your text is set', log=False)
else:
    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?

Best,  
Robin

---

<div class="post-metadata">

### Author: ![Ashish](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@Ashish](https://discourse.psychopy.org/u/Ashish)
#### Post date: [October 24, 2017, 8:21pm UTC](https://discourse.psychopy.org/t/only-display-text-under-certain-conditions/3301/4 "2017-10-24T20:21:21Z")

</div>

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

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

```

in a code component Each Frame as per your suggestion schubisu

Thanks guys!
