Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Slider widget in Expyriment

Hello,
I would like to build a slider widget to get ratings of images under Expyriment. The idea is to get a score from the subject regarding images on display for ~5sec. The participant has to press a button to slide left and another one to slide right in order to set the cursor in a specific position related to the value of their score. Would it be possible to get a demo snippet of code on how to build this under Expyriment?

Thank you very much in advance.
Best,
Ana Luísa

Comments

  • Hi anapinho,

    not a slider widget, but a similar solution that works with radio buttons:

    #!/bin/env python
    
    """ A radiobutton example in Expyriment."""
    
    __author__ = "Florian Krause <florian@expyriment.org>"
    
    
    from expyriment import control, stimuli, misc
    
    
    control.set_develop_mode(True)
    exp = control.initialize()
    exp.mouse.show_cursor()
    
    question = stimuli.TextScreen("The answer to life, the universe and everything?","")
    question.present()
    question.present() # Twice for double buffering!
    
    answers_pos = [["1", -300], ["2", -200], ["3", -100], ["42", 0], ["7", 100], ["99", 200], ["100", 300]]
    
    answer_canvas = stimuli.Canvas((700, 200))
    answers = []
    buttons = []
    for answer in answers_pos:
        a = stimuli.TextLine(answer[0], position=(answer[1], -30))
        a.plot(answer_canvas)
        b = stimuli.Rectangle((20, 20), line_width = 2, position=(answer[1], 0))
        b.ticked = False
        b.plot(answer_canvas)
        buttons.append(b)
    
    answer_canvas.present(clear=False)
    answer_canvas.present(clear=False)  # Twice for double buffering!
    
    tick = stimuli.Rectangle((10, 10))
    tick.preload()
    
    untick = stimuli.Rectangle((10, 10), colour=exp.background_colour)
    untick.preload()
    
    while True:
        exp.keyboard.check()  # Press ESC to exit
        if exp.mouse.check_button_pressed(0):
            exp.mouse.wait_press(0, wait_for_buttonup=True)
            pos = exp.mouse.position
            for button in buttons:
                if button.overlapping_with_position(pos, mode="surface"):
                    if button.ticked:
                        button.ticked = False
                        untick.position = button.position
                        untick.present(clear=False)
                        untick.present(clear=False)  # Twice for double buffering!
                    else:
                        button.ticked = True
                        tick.position = button.position
                        tick.present(clear=False)
                        tick.present(clear=False)  # Twice for double buffering!
    
    

    You could modify it to work with the keyboard instead of with the mouse.

    Hope this helps!

    Florian Krause (Developer)
    http://www.expyriment.org

  • Thank you for this. In the end, I decided for a different solution but I will keep this for the future.

    Thanked by 1PMKraemer
  • Hello anapinho,
    would you be so kind to tell how you did it? I'm facing a very similar problem than you did. Would be super helpful to get some idea :smile:

  • okay I've approached my problem for a willingness-to-pay task for a snack like this:

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    """
    Created on Wed May  9 17:20:15 2018
    
    @author: peterkraemer
    """
    
    from expyriment import control, stimuli
    
    #control.set_develop_mode(True)
    
    exp = control.initialize()
    ######################  STIMULI  ######################
    #   creating elements on screen
    
    # picture
    path_picture =   '/Users/peterkraemer/Desktop/experiment/PICTURES/snack1.bmp'
    picture      =   stimuli.Picture(path_picture, position=(0,200))
    
    # bar
    barLength = 300
    barThick = 10
    posBar=(0,-100)
    bar = stimuli.Rectangle((barLength,barThick),corner_rounding=None,position=posBar)
    bar.preload()
    barEndLeft = stimuli.Rectangle((5,barThick*3),position=((-(barLength+2)/2), posBar[1]))
    barEndLeft.preload()
    barEndRight = stimuli.Rectangle((5,barThick*3),position=(((barLength+2)/2), posBar[1]))
    barEndRight.preload()
    
    # slider
    posSlider = (0,posBar[1]) # initial position of the slider
    
    # sliding function
    def sliding(pos,commit):
        while commit==0:
            # create a composition to show multiple elements at the same time
            composition = stimuli.BlankScreen()
            picture.plot(composition)
            bar.plot(composition)
            barEndLeft.plot(composition)
            barEndRight.plot(composition)
            slider = stimuli.Rectangle((3,30),position=pos, colour = (194, 24, 7))
            slider.plot(composition)
            composition.present()
            # participants can use left and right arrow to adjust slider position & commit with enter
            key, rt = exp.keyboard.wait(process_control_events=True)
            if key == 13:
                commit = 1
                return pos[0]
            elif key == 275:
                pos = (pos[0]+10, pos[1])
                if pos[0] > barLength/2: # fixes upper end of the scale
                    pos = (barLength/2,pos[1])
            elif key == 276:
                pos = (pos[0]-10, pos[1])
                if pos[0] < -barLength/2: # fixes lower end of the scale
                    pos = (-barLength/2,pos[1])
    
    # position - money translation
    scaleMax = 5 # upper end of the scale
    moneyPerPixel=scaleMax/barLength # = CHF/Pixel
    
    ## start ##
    control.start(exp,skip_ready_screen=True)
    
    val = (sliding(posSlider,0)-(-barLength/2))*moneyPerPixel
    print(val)
    
    ## end ##
    control.end()
    

    certainly not the most elegant way but anywayne :#

Sign In or Register to comment.