Add button to slider on canvas
Hi,
I added a slider to a canvas. Now i want to add a button in the center of my canvas just below the slider.
I want my participants to either set the slider to a specific position or press the button. The button should be labeled "no answer".
My inline script is as follows:
my_canvas = canvas() my_mouse = mouse(timeout=20) # Set slider dimensions. This assumes that 0,0 is the display center, which is # the default in OpenSesame >= 3. slider_w = 500 slider_h = 10 slider_x = -slider_w/2 slider_y = -slider_h/2 while True: # Determine the slider fill based on the mouse position pos, time = my_mouse.get_pos() x, y = pos slider_fill = min(slider_w, max(0, x-slider_x)) my_canvas.clear() # Draw some text (this can be anything) my_canvas.text("angewidert", y=slider_y-100) my_canvas.text("nicht verstanden", y=slider_y+100) my_canvas.text("extrem intensiv", x=slider_x+600) my_canvas.text("überhaupt nicht intensiv", x=slider_x-100) # Draw the slider frame my_canvas.rect(slider_x, slider_y, slider_w, slider_h) # Draw the slider fill my_canvas.rect(slider_x, slider_y, slider_fill, slider_h, fill=True) # Draw the mouse cursor my_canvas.arrow(x+5, y+10, x, y) my_canvas.show() # Poll the mouse for buttonclicks button, position, timestamp = my_mouse.get_click() if button is not None: break # Set the slider response as an experimental variable var.slider_percent = 100.0*slider_fill/slider_w
Can you help? Thank youuuu
Comments
Hi @mirix ,
Right now you're using a
Canvas
to draw a custom slider instead of aForm
. That's a perfectly acceptable approach, but this means that you have to render everything using theCanvas
, including the ok button. (You cannot use any of the predefined form widgets.) The script below is slightly modified from your own script and shows how you can do this. There are three major changes:Canvas
is not cleared before each update. Rather, the slider fill is given a name and then updated selectively while the other elements are left unchanged.See also:
Do you see the logic?
— Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!