Welcome!

Sign in with your CogSci, Facebook, Google, or Twitter account.

Or register to create a new account.

We'll use your information only for signing in to this forum.

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

  • fladdfladd Posts: 71

    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!

    Thanked by 1anapinho

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

  • anapinhoanapinho Posts: 11

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

Sign In or Register to comment.