[open] Text input function - add maximum & minimum
Hello,
I'd like to clamp the text input value, so that if participants go above a maximum value or go below a minimum value the text_input prints the the max or min values.
I added in this code in a separate inline script, but it still logs the original values:
# add max and min values
min_num = 0.50
max_num = 100
def clamp(text_input, min_num, max_num):
if text_input < min_num:
return min_num
elif text_input > max_num:
return max_num
else:
return text_input
text_input = clamp(text_input, min_num, max_num)
I also tried it in the same inline script as the text input function:
#import widgets
from libopensesame import widgets
# set columns and rows
form = widgets.form(self.experiment,cols=1, rows=3)
# add instruction
label = widgets.label(form, text='How much are you willing to offer in steps of 50 Cents?<br /><br />Click inside the box, type the amount and press ENTER.')
# add image
image = widgets.image(form, path=self.experiment.get_file(self.get('video_image')))
# add text input box
text_input = widgets.text_input(form, var='response', return_accepts=True)
# add max and min values
min_num = 0.50
max_num = 100
def clamp(text_input, min_num, max_num):
if text_input < min_num:
return min_num
elif text_input > max_num:
return max_num
else:
return text_input
text_input = clamp(text_input, min_num, max_num)
# set order of widgets
form.set_widget(label, (0,0))
form.set_widget(image, (0,1))
form.set_widget(text_input, (0,2))
form._exec()
But it kept coming up with the error:
Error while executing inline script
phase: run
item: Responder
line: 314
exception message: 'int' object has no attribute 'set_rect'
exception type: AttributeError
Traceback (also in debug window):
File "dist\libopensesame\inline_script.py", line 173, in run
File "dist\libopensesame\python_workspace.py", line 111, in _exec
File "<string>", line 34, in <module>
File "dist\libopensesame\widgets\_form.py", line 315, in set_widget
AttributeError: 'int' object has no attribute 'set_rect'
Thanks in advance
Comments
Hi Hollana,
My apologies, but it's not really clear to me what you want to do. The code you copied here,
What do you exactly hope for this code to do?
I guess you want people to indicate an amount, and just give a kind of warning when this amount is not within the given boundaries? Something like that can be easily implemented; however I need to know what strategy you were aiming for before I can point you in the right direction.
Cheers,
Josh
Hi Josh,
Thanks for the reply. Sorry for the confusion.
I'd like people to indicate an amount, but if that amount falls below a minimum or maximum number then a warning should come up and they have to change the amount.
Cheers!
Alright!
Did you already manage to get this to work without the minimum/maximum message? What you could do is place the form item in a loop. Clicking on the loop item, you can tick an 'advanced options' box to reveal a break-if statement. Here you can make sure that your form is repeated until participants give a response that is within your desired boundaries - for instance: break if: =self.get('done')==True.
Now inside the loop item, you have to place a second form, which is a copy of the first one but with the addition of your warning message. (By the way, the loop item only takes one item; hence you need to place a sequence item in the loop item, and add both forms to the sequence). The form with a warning message should only be played if the response was below the minimum or above the maximum. Clicking on the sequence item, you can define a run-if statement for this form: run if: [response]maximum. Further, the first form (without warnings) should only be run the first time, so here you can insert: [first_time] = True
What you then need to do is alter the variable 'done' based on what people responded. So you'd place an inline_script at the end of your sequence, and insert the commands:
Lastly, the experiment will crash if it runs all of the above run-if statements while there is no such existing variable. Frankly, this is the case with the first iteration through the loop, since both 'done' and 'first_time' are made in the inline_script. Thus, you'd only need to place an additional inline script somewhere before the loop, and create these variables:
Let me know how it goes.
Cheers,
Josh