Failing to update text element via code snippets

What do I want to do?
I have an experiment where I want to give participants the opportunity to repeat 5 practice trials. To this end, I have two clickable buttons for [continue] or [repeat] in a component I call “ready”. In addition to those buttons, I have some text which should naturally change depending on whether it is the first presentation of “ready”, the second, or the one after clicking [continue].
It should work like this:
1st block: 5 practice trials with their separate conditions file.
2nd block: Either 90 trials of experiment proper with their own conditions file or repetition of 5 practice trials.
3rd block (only, when practice was repeated): experiment proper as described above.

How did I approach this?
Within the “ready” component, I added the following code:

# Before experiment
main_exp = False

# Begin routine
# set text depending on block
if thisBlock == 1:
    ready_txt = visual.TextStim(win=win, name='ready_txt',
        text='Jetzt beginnen wir mit 5 Übungsdurchgängen.',
        font='Open Sans',
        pos=(0, 0), height=0.05, wrapWidth=None, ori=0.0, 
        color='white', colorSpace='rgb', opacity=None, 
        languageStyle='LTR',
        depth=0.0);
elif thisBlock == 2:
    ready_txt = visual.TextStim(win=win, name='ready_txt',
        text='Wenn du bereit bist, klicke auf [weiter]. Mit [nochmal] kannst du die Übungsdurchgänge wiederholen.',
        font='Open Sans',
        pos=(0, 0), height=0.05, wrapWidth=None, ori=0.0, 
        color='white', colorSpace='rgb', opacity=None, 
        languageStyle='LTR',
        depth=0.0);
elif main_exp == True: # This elif is not an else, because I need to check whether the Ifs work at all.
    ready_txt = visual.TextStim(win=win, name='ready_txt',
        text='Jetzt geht es richtig los!',
        font='Open Sans',
        pos=(0, 0), height=0.05, wrapWidth=None, ori=0.0, 
        color='white', colorSpace='rgb', opacity=None, 
        languageStyle='LTR',
        depth=0.0);

# End routine
if mouse.isPressedIn(again_button, buttons=[0]):
    num_trials = 5
    condfile = 'practice_exp_'+seq+'.xlsx'
elif mouse.isPressedIn(continue_button, buttons=[0]):
    num_trials = 90
    condfile = 'learn_exp_'+seq+'.xlsx'
    main_exp = True

What does my experiment look like?

What is the problem?
Instead of showing the conditional text I set with code in Begin Routine, it shows this:

Please help, I don’t get it. Could it be that I need to redraw the text element or something like that? If so, how? I couldn’t find anything in the internet or used the wrong keywords. Thank you!

I have no experience yet with routines and the builder interface myself (I’ve only used the PsychoPy module directly in Python), so this is just a wild guess, but could you try to set the text using the following possibilities:

ready_text= "Jetzt ..."
or
ready_text.text = "Jetzt ..."

Hi Christophe,

Thanks for the suggestion. I tried both - they did not work. It still looks the same. If it helps, I could upload the whole Python code. Maybe I should also re-categorize this thread under Builder. I was unsure, because it’s a mix of clicking stuff together and coding.

Best,
Jasmin

Hello,

using Builder you can set text in the following manner. Use a code-component to set the text

if thisBlock == 1:
   read_txt = "Jetzt beginnen wir mit 5 Übungsdurchgängen."
elif thisBlock == 2:
   read_txt = "Wenn du bereit bist, klicke auf weiter."

Then add a text-component and refer to the text with $read_text, set it to set every repeat

Best wishes Jens

Hi Jens,

Thank you. This is actually what I tried at first, before I added the full detail specifications of read_txt. I had tried it with singular quotation marks though. I just tried it with double quotation marks, exactly as you wrote, but I still only see code instead of the captions I specified. I also tried

ready_txt.setText("Jetzt ....")

but it also didn’t work.

The weird thing is that I do not even find this specific code (see screenshot of my problem) in my Python code (when transforming all Builder elements to code). Does this code mean anything to you?

Is your code component above your text component in your routine?

Perhaps you could show screenshots of your text component, your code component and your routine.

Hi @kizilirmak,
what you see on the screen is the text representation of a TextStim object. Most likely the TextStim object that you create (ready_txt) is passed as a variable to text object created in the builder. Because your ready_txt variable is a TextStim and not str object it is interpreted as text and this text representation of TextStim is what you see. It should be enough if you just do:

if thisBlock == 1:
    ready_txt = 'Jetzt beginnen wir mit 5 Übungsdurchgängen.'
elif thisBlock == 2:
    ready_txt = 'Wenn du bereit bist, klicke auf [weiter]. Mit [nochmal] kannst du die Übungsdurchgänge wiederholen.'

# and so on...

just make sure that the builder text object that shows the text has $ready_txt in the Text field and it is set to “set every repeat”.

Oh, sorry, this has been suggested here before.
Single or double quotation marks do not matter BTW.
If this does not change what you see on the screen, then make sure that the text component in builder has a different name from ready_txt and the code block is above the text component (you can move it up by right clicking and choosing relevant option).
The problem you see likely stems from the fact that you Builder text object is named ready_txt and it is in fact referencing itself in the Text field.

using .setText() should also work, but then you would have to remove $ready_txt from the Text field of the builder text component.

Hi There,

I would also suggest the approach taken by Jens Failing to update text element via code snippets - #4 by JensBoelte

In terms of your question here “The weird thing is that I do not even find this specific code (see screenshot of my problem) in my Python code (when transforming all Builder elements to code). Does this code mean anything to you?” Yes, so the code you see in your screenshot just means this is a TextStim object that has been created and exists. You make the TextStim object when you declare it like this ready_txt = visual.TextStim(win=win... So it means you are putting a textstim object in the text field of another textstim object (your component) rather than inserting just plain text.

What happened when you tried your first method of just using text?

Thanks,
Becca

The same. But I think mmagnuski is right: I stupidly named the Builder text object the same as my text variable! When I rename the Builder text element, my script crashes at this component, suggesting that I have a new problem at hand now. I will get back, once I have debugged that (or when I need help), and write whether the problem with the text updating has been solved by the renaming.

I managed to get it to work. It was really a problem of two objects having the same name. It’s a pity there was no error like “There is already a text object with the same name.”. The code during Begin Routine now looks like this:

if blocks.thisN == 0:
    num_trials = 5
    condfile = 'practice_exp_'+seq+'.xlsx'
    ready_txt = "Jetzt beginnen wir mit 5 Übungsdurchgängen."
elif blocks.thisN == 1:
    ready_txt = "Wenn du bereit bist, klicke auf [weiter]. Mit [nochmal] kannst du die Übungsdurchgänge wiederholen."
elif main_exp == True or blocks.thisN > 1:
    ready_txt = "Jetzt geht es richtig los!"
    again_button.opacity = 100
    again_txt.opacity = 100
else:
    print('If-clause did not work. Current block value is ' + str(blocks.thisN))

Outcome:

I now have a different problem (number of loops is a variable that changes conditionally, but PsychoPy ignores the value (I printed it, the value is correct) and loops forever), but that’s for a different topic.

Hi there,

Pleased you managed to fix your original problem! Yes do post a different topic for the new issue and hopefully we can help.

Becca