Howdy, Stranger!

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

Supported by

[solved] Implementing a Slider

edited October 2011 in OpenSesame

Hello,

I am hoping to run a study in OpenSesame where subjects will hear a speech sample and then use a slider to rate naturalness, intelligibility, etc. Does anyone have any insight on how I might to this. I have found python code that runs a slider widget (http://effbot.org/tkinterbook/scale.htm), but it does not work very well when I try to use the 'inline_script' function (the widget will pop up but you can only see it when you exit the experiment). Thank you in advance.

Comments

  • edited October 2011

    Hi Mpaullin,

    Welcome to the forum and thank you for your interest in OpenSesame!

    The problem with using widgets (Tk, Qt, Gtk, and what have you) is that they typically draw on their own window, and not on the OpenSesame window. This is why you see the widget only after the experiment closes (and perhaps in a separate window if you don't run the experiment fullscreen?)

    There are a number things you can do:

    • The questionnaire plug-ins have a rating scale. If this suits your needs, this is probably the best and easiest way to go. http://osdoc.cogsci.nl/plug-ins/questionnaire-plug-ins

    • If you really need a slider, you will basically have to program one in Python inline code. But this is not that hard to do. If you tell me specifically what you want to do, I can give you some pointers.

    Hope this helps!

    Kindest regards,
    Sebastiaan

  • edited 3:43PM

    Thanks a lot for your helpful reply. I took a look at the rating scale plugin, and I'm afraid that it does not meet my needs. I'm looking for something that can give me a lot more variability, which is why a slider would work best. In the past we used a slider with about 200 underlying data points. I am not a programmer in any sense, so would appreciate any tips you may have for implementing a slider using python inline code. Thanks again

  • edited 3:43PM

    Here's a small snippet that shows a mouse-controlled slider. You will probably want to modify it a bit, but hopefully it conveys the general idea. You can find more about the canvas functions (for drawing) here: http://osdoc.cogsci.nl/python-inline-code/canvas-functions

    You will need to paste this code into the run phase of an inline script item. I've tested it with OpenSesame 0.25-pre9, but it should work with 0.24 as well.

    from openexp.canvas import canvas
    from openexp.mouse import mouse
    my_canvas = canvas(self.experiment)
    my_mouse = mouse(self.experiment, timeout=20)
    
    # Set slider dimensions
    slider_w = 500
    slider_h = 10
    slider_x = self.get("width")/2-slider_w/2
    slider_y = self.get("height")/2-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("Question goes here", y=slider_y-100) 
        my_canvas.text("Click to accept ...", y=slider_y+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(timeout = 20)
        if button != None:
            break
        
    slider_percent = 100.0*slider_fill/slider_w     
    self.experiment.set("slider_percent", slider_percent)
  • edited 3:43PM

    That worked beautifully. Thanks again for your help.

  • edited 3:43PM

    This one's great, thanks a bunch! This could be a bit of a silly question, but I'm going to risk making a slight fool of myself: where can I adjust the colours of the text and maybe the background as well? Did I miss it in your code?

    Thanks in advance!

    Edwin

  • edited December 2011

    Hi Edwin,

    You can use either the set_fgcolor() and set_bgcolor() functions, which will affect all subsequent drawing operations, or pass the 'color' keyword for a single drawing operation. So the following (adapted from above) will give you a horribly coloured slider:

    ...
    my_canvas.set_bgcolor('green')
    my_canvas.clear()
    # Draw some text (this can be anything) 
    my_canvas.text("Question goes here", y=slider_y-100, color='blue') 
    my_canvas.set_fgcolor('red')
    my_canvas.text("Click to accept ...", y=slider_y+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()
    ...

    For a complete list of canvas functions, see this page: http://osdoc.cogsci.nl/python-inline-code/canvas-functions

    Hope this helps!

    Regards,
    Sebastiaan

  • edited 3:43PM

    Hi Sebastiaan,
    That seems to work, thanks a bunch!

  • Hi,

    i am trying to make a slider working from keyboard keys RIGHT and LEFT, for say.
    I cannot use the mouse as i am in the MRI scanner.
    I dont find any way to distinguish the keyboard presses from their release (to smooth the slider movement).. is there one, somehow ?

    Thanks
    PE

  • Hi,

    This code is just what we were looking for, but it's quite laggy at runtime (a few seconds, the mouse tends to be jumpy). Do you have any hints?

  • You can try different backends. Which one do you use right now?

    Btw. the discussion here is rather old, which code did you use eventually? And did you have to tweak it?

    Eduard

    Buy Me A Coffee

  • edited February 2018

    We're using xpyriment as the backend, and here is what we did with the code we're using:

    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("Please rate the text you just read on naturalness", y=slider_y-100) 
        my_canvas.text("Click to accept rating...", y=slider_y+100) 
        my_canvas.text("Most natural", x=slider_x+600)
        my_canvas.text("Least natural", 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    
    
  • edited February 2018

    Hi,
    Here an optimized version of the slider. Hope it is sufficient.

    my_canvas = canvas()
    my_mouse = mouse(timeout=20)
    my_mouse.show_cursor(True)
    # 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
    my_canvas.text("Please rate the text you just read on naturalness", y=slider_y-100) 
    my_canvas.text("Click to accept rating...", y=slider_y+100) 
    my_canvas.text("Most natural", x=slider_x+600)
    my_canvas.text("Least natural", x=slider_x-100)
    # Draw the slider frame
    my_canvas.rect(slider_x, slider_y, slider_w, slider_h)
    lifeCanvas = canvas()
    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))
    
        lifeCanvas.clear()
        my_mouse.set_visible()
    
        lifeCanvas.copy(my_canvas)
        # Draw some text (this can be anything)
    
    
        # Draw the slider fill
        lifeCanvas.rect(slider_x, slider_y, slider_fill, slider_h, fill=True)                
        # Draw the mouse cursor
        lifeCanvas.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    
    

    Eduard

    Buy Me A Coffee

  • edited April 2018

    Hi all,

    I'm doing a mouse tracking experiment (with the Mousetrap plug-in) using the visual analogue scale (slider) based on the code above (by Eduard). Before that I used this code:

    # import everything we need
    from openexp.canvas import canvas
    from openexp.mouse import mouse
    
    # create the instances we need
    my_canvas = canvas(self.experiment)
    my_mouse = mouse(self.experiment, timeout=1)
    
    # slider dimensions
    slider_w = 500
    slider_h = 10
    slider_x = -slider_w/2
    slider_y = -slider_h/2
    
    # draw some text (this can be anything)
    my_canvas.text("To what extent were your thoughts focused on or off the task just before this moment?<br><br>Please indicate a percentage where 100% indicates completely on-task and 0% completely off-task.", y=slider_y-200, font_size=30)
    #draw options
    my_canvas.text("0%<br>(Completely off-task)", y= slider_y+50, x= -250, )
    my_canvas.text("100%<br>(Completely on-task)", y= slider_y+50, x= 250)
    # draw the slider frame
    my_canvas.rect(slider_x, slider_y, slider_w, slider_h)
    # add a text below the bottom slider, telling participants to click to accept
    my_canvas.text("Click your mouse to accept ...", y=slider_y+200) 
    
    # keep showing the interactive slider until a click has been made
    clicked = False
    while not clicked:
    
        # 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))
            # draw an empty rect within the slider frame, effectively resetting it
        my_canvas.rect(slider_x, slider_y, slider_w, slider_h, fill=True, color=self.get("background"))
            # draw the slider fill
        my_canvas.rect(slider_x, slider_y, slider_fill, slider_h, fill=True)
            # show the updated canvas
        my_canvas.show()
        # Poll the mouse for buttonclicks
        button, position, timestamp = my_mouse.get_click(timeout = 1)
        if button != None:
            clicked = True
    
    
    # save the slider's filling
    slider_percent = 100.0*slider_fill/slider_w  
    exp.set("slider_percent", slider_percent)
    

    However, with the code by Eduard, sometimes my stimuli do not show at random trials. When I used the code in this comment this didn't happen and it's the only thing I have changed.

    So the stimuli do not show, but the buttons that I made with the Mousetrap plug-in at the same place of the stimuli are still clickable. The experiment then moves to the next trial and there the stimuli do show.

    I really don't know if this is because of the change in code for the slider, but I hope someone can help me with this.

    Best,

    Dirk

  • Hi

    I'm totally new to open Sesame and Python and am putting together an experiment which will ask participants to rate the emotional intensity of a series of images. I'd like to use a slider instead of manually inputing a 1-9 scale. The optimised version posted above by Eduard works perfectly but is there any way to include the slider on the sketchpad I'm using to show the image and have the program advance to the next image when the mouse is clicked? I'd also like the slider to reset to zero each time, at the moment it stays at the same point as the previous response, which might tempt participants just to click in the same spot.

    Any help would be gratefully received
    thanks
    Elise

  • Hi @EliseF,

    I would take a look at the same link that I suggested to Dirk above. This provides a slider widget that you can embed in a form, along with other widgets, such as images.

    Cheers!
    Sebastiaan

  • Hi people,

    I'm using the code recommended here for a slider in an experiment i'm building.

    My problem is that it works perfectly well when I run it in window but when I try to run it in full screen the mouse won't run on the entire slider and would just kind of stay stuck around the midpoint of the scale.

    Any ideas on how to fix this?

    Thanks!

  • HI @mikapita,

    It sounds like the cursor position is reset to the center, which happens on systems when the cursor is hidden and then shown again. You can probably change this behavior by going to the backend settings (in the General Properties tab) and trying out different options under the Canvas backend.

    Alternatively, you can take a look at the discussion below, which provides a more recent implementation of a slider widget.

    Cheers!

    Sebastiaan

  • Hi everyone,

    I have modified the inline script for slider according to my experiment, and it works very good.

    But I still have difficulty to add/make timeout for this slider. In my experiment, the participant should press the slider within 10 seconds, or if they are not pressing the slider, the screen will change automatically after 10 seconds.

    I attached the file here, please no need to pay attention to the text stimuli because the stimuli is in Indonesian language/is'nt in English).

    I really hope someone can give help for this case.

  • Hi Lisa,

    Attached your experiment, with a timeout added. I hope this helps,

    Eduard


    Buy Me A Coffee

  • I am grateful, the script works very good. Thanks a bunch Eduard!

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games