Howdy, Stranger!

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

Supported by

Changing image on keypress

Hi,

I am trying to program a test trial where participants can change the image that is displayed on the screen.

To choose the image I would really like for them to scroll through a set of images by pressing a button (e.g. press 'a' to show img_x and press 'l' to show img_y) and then when they decide press 'enter' to move on.

I am trying to do something similar to the BART experiment, i.e. where a participant presses a button such as 'spacebar' and the image increases in size. But instead of increasing in size, during a trial the actual image changes. Once the participant has decided on which image they want, they then press 'enter' and the specific image choice is logged. I have 10 images, named 1.png, 2.png etc.

I know this will require some in-line coding, with if statements, but I just can't figure out how to do it, my programming skills are beginner only.

Any help would be much appreciated!

Thanks,

James

Comments

  • Hi James,

    Sounds like fun. Here my implementation. Let me know if it does what you want.

    import random
    
    cv = canvas() # create an empty canvas
    kb = keyboard(timeout = 20,keylist = ['a','l','return']) # create a keyboard item that allows for 3 different keys
    
    letters = [1,2,3,4,5,6,7,8] # my stimuli, I don't have your images.
    
    random.shuffle(letters) # randomize images
    
    for i in range(len(letters)-1): # loop over each comparison, I don't knwo whether this is what you had in mind, if not you can remove this for loop 
        var.selected_item = None # initliaze the item that will be selected
        while True: # stay in the loop until a selection was done
            cv.text(letters[i],x = -200) # draw one stimulus to a canvas
            cv.text(letters[i+1],x = 200) # draw the other to the canvas
            cv.show() # shwo the canvas
            if var.selected_item == True:
                cv.clear() # clear the canvas, if no selection has been made yet
            k,t=kb.get_key() # sample a key response
            if k == 'a': # if a was pressed (left stimulus)
                cv.clear() # clear the canvas and...
                cv.rect(-250,-50,100,100) # draw a rectangle around the stimulus to mark it as selected
                var.selected_item = letters[i] # store selection in variable
            elif k == 'l': # same for the right stimulus
                cv.clear()
                cv.rect(150,-50,100,100)
                var.selected_item = letters[i+1]
            elif k == 'return': # if return pressed, break the loop and clear the canvas
                cv.clear()
                break
    
    

    I made it with letters because I haven't had images, but it should be doable to adjust. Basically get the path for each image like so: path = exp.pool[u'image_in_pool.png'], and store all the paths in a list. And later draw the images with my_canvas.image(path,x=x) instead of with canvas.text(). See also here: http://osdoc.cogsci.nl/3.1/manual/python/canvas/#function-canvas46image40fname-centertrue-xnone-ynone-scalenone41

    Good luck.

    Eduard

    Buy Me A Coffee

Sign In or Register to comment.