psychopy.org | Reference | Downloads | Github

Inserting numbers in text box

Hi everyone! I am quite new to the forum and this is my first time posting. I am using Windows 10 and PsychoPy 3.8 and I am working on letting participants fill in a number between 0 and 1000. I am using the following code, where text_neutral is my empty text box. It seems to work partially but I cannot get it to work so that you cannot fill in the numbers “1001” “1002” “1003” and so on. Participants are also seeing what they are typing in.

if len(keys):
if len(text_neutral.text) > 2:
if (text_neutral.text) == “100”:
continueRoutine = True
if len(text_neutral.text) > 3:
if (text_neutral.text) == “1000”:
text_neutral_text = text_neutral.text[0:3]
else:
text_neutral.text = text_neutral.text[:-1]
else:
text_neutral.text = text_neutral.text[0:2]
if len(text_neutral.text) > 3:
text_neutral.text = “1000”
if len(text_neutral.text) > 4:
text_neutral.text = text_neutral.text[0:2]
elif ‘backspace’ in keys:
text_neutral.text = text_neutral.text[:-2]
elif ‘return’ in keys and len(text_neutral.text) < 1:
continueRoutine = True
elif ‘return’ in keys and len(text_neutral.text) > 4:
continueRoutine = True
elif ‘return’ in keys and len(text_neutral.text):
allowed_keys = []
else:
text_neutral.text = text_neutral.text + keys[0].replace(‘num_’, ‘’)

There was no specific error that came up, I just cannot get it to stop when somebody tries to tick in “numbers” between 1001 and 1009.

I hope that somebody can shed some insight.

Best,

Snoek

Hi @Snoek,

could you copy the code in a way that preserves the indentation? You used the “Preformatted text” option but you didn’t copy the formatting :smiley:

allowed_keys = ['1','2','3','4','5','6','7','8','9','0', 'return', 'backspace', 'num_1', 'num_2', 'num_3','num_4','num_5','num_6','num_7','num_8','num_9','num_0']
keys = event.getKeys(keyList= allowed_keys)


if len(keys):
    if len(text_ss1.text) > 2:
        if (text_ss1.text) == "100":
            continueRoutine = True
        else:
            text_ss1.text = text_ss1.text[0:2]
    if len(text_ss1.text) > 3:
        if (text_ss1.text) == "1000":
                text_ss1.text[0:3]
        else:
                text_ss1.text = text_ss1.text[:-2]
    if len(text_ss1.text) > 4:
        text_ss1.text = text_ss1.text[0:4]
    elif 'backspace' in keys:
        text_ss1.text = text_ss1.text[:-2]
    elif 'return' in keys and len(text_ss1.text) < 1:
        continueRoutine = True
    elif 'return' in keys and len(text_ss1.text) > 4:
        continueRoutine = True
    elif 'return' in keys and len(text_ss1.text):
        allowed_keys = []
    else:
        text_ss1.text = text_ss1.text + keys[0].replace('num_', '')

Hi @LukasPsy

Apologies for this! I must have double-copied and pasted it.

Thank you for the heads up.

Best,

Snoek

if len(keys):
    if len(text_ss1.text) > 3 and (text_ss1.text) != "1000": # remove the last digit if the number is longer than 3 digits and it is not 1000
        text_ss1.text = text_ss1.text[:-2]
    
    if ('backspace' in keys): # ceck for backspace
        text_ss1.text = text_ss1.text[:-2]
    elif 'return' in keys and len(text_ss1.text) > 1: # check for return and end the routine if at leas one digit has been entered
        continueRoutine = True
    else: # add to the number that is displayed
        text_ss1.text = text_ss1.text + keys[0].replace('num_', '')
1 Like

Thank you @LukasPsy for your answer and for taking the time to look at it.

When I implement this, the very same problem occurs. I, as a participant, can insert values ranging from 1001 to 1009: 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009.

I actually think I tried using this exact code myself before as well:

    if len(text_ss1.text) > 3 and (text_ss1.text) != "1000": # remove the last digit if the number is longer than 3 digits and it is not 1000
        text_ss1.text = text_ss1.text[:-2]

It would make sense for it to work, but somehow I can still fill in “1001” and so on.

I am still at a loss.

Best regards,

Snoek

Okay, I must have overlooked something. Could you try this:

if len(keys):
    if ('backspace' in keys): # check for backspace
        text_ss1.text = text_ss1.text[:-1]
    elif 'return' in keys and len(text_ss1.text) > 1: # check for return
        continueRoutine = True
    else: # add to the number that is displayed but only if the new key does not make the number larger than 1000
        newNumStr = text_ss1.text + keys[0].replace('num_', '')
        if int(newNumStr) <= 1000:
            text_ss1.text = newNumStr
1 Like

Hi @LukasPsy,

It works perfectly. I cannot thank you enough. Hopefully, I can return the favour at one point!

Kind regards,

Snoek

1 Like