Howdy, Stranger!

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

Supported by

Setting mouseclick durations inline script

edited June 2023 in OpenSesame

I am attempting to set up a task whereby I pull in a scrollable image, using the following inline script syntax

import webbrowser
webbrowser.open_new(r'C:filepath\Test_doc.pdf')

I’m able to successfully have the script pull up my image file. However I also want the duration of this image file to be a mouseclick (as one would do with a sketchpad), after which, the next part of the experiment (either another inline script, or a sketchpad) appears. I’m having trouble getting this to be the case. I’ve looked at the scripting that is available for logging mouse clicks such as:

from openexp.mouse import mouse
my_mouse = mouse(exp)
button, time = my_mouse.get_click(buttonlist=[1,3], timeout=3000)

But I cannot seem to get it to be the case whereby my experiment moves forward to the next part of my sequence when a click occurs, once my image is pulled up, the experiment simply stays on the image. Any help would be greatly appreciated, thank you! 

Comments

  • Hi @sdewitt ,

    Your script opens a PDF file in a browser window, which is separate application. At that point, you're no simply longer in OpenSesame--you've left the experiment and need to switch back to it in order to continue.

    If you want to have a scrollable image, then there are various ways to do so within OpenSesame. One way is to use a short inline_script that modifies the y coordinate of an image element, a file called image.png taken from the file pool, on a Canvas based on mouse input, like so:

    my_canvas = Canvas()
    my_canvas['img'] = Image(pool['image.png'])
    my_mouse = Mouse()
    while True:
      my_canvas.show()
      button, pos, timestamp = my_mouse.get_click()
      # Break loop on left click
      if button == 1:
        break
      # Scroll up
      if button == 4:
        my_canvas['img'].y -= 10
      # Scroll down
      elif button == 5:
        my_canvas['img'].y += 10
    

    You will probably need to modify this script a bit, but hopefully it will get you started!

    -- Sebastiaan

  • edited June 2023

    Hi @sebastiaan


    Thank you so much for your quick response! I suspected my code was navigating outside of opensesame, based on how the image was being presented. Editing and using the script you have above, I'm still having a few issues. See my code after the description of my issue and my error message.


    Namely, it seems my opensesame backend does not have the canvas, image, or pool modules (the backend right now is expyriment, I don't actually know how to change that to something like psychopy). As such, I start the script out by importing multiple modules (which I think is not ideal). Even If I just do this in the prepare, not run (or if I do it in both), I get the subsequent error message on line 4 (note: 1. if I don't import the modules, the script breaks on each line the modules are called. 2. my_canvas = canvas() breaks without an input for canvas(), hence why I have canvas(exp), I'm not sure if this is accurate.

    phase: prepare

    item: ___inline_script

    line: 4

    exception message: name 'pool' is not defined

    exception type: NameError


    from openexp.canvas import canvas

    from PIL import Image

    my_canvas = canvas(exp)

    my_canvas['img'] = Image(pool['image.png'])

    my_mouse = Mouse()

    while True:

     my_canvas.show()

     button, pos, timestamp = my_mouse.get_click()

     # Break loop on left click

     if button == 1:

    break

     # Scroll up

     if button == 4:

    my_canvas['img'].y -= 10

     # Scroll down

     elif button == 5:

    my_canvas['img'].y += 10

  • Hi @sebastiaan,


    I was actually able to work through the errors in the comment I made above (it wouldn't let me edit the unnecessary comment). I had to use a work around with exp to get the image from my file pool, but I've successfully done so (see below), and it responds to the break with a mouse response (1, left button click). The struggle I now have is,

    1. since the way I pulled the file into my canvas is different than how you did, I can't figure out the correct syntax if/elif for button ==4/5 part of the code. since my code doesn't have a my_canvas['img'] to append .y to. I've tried appending .y to what I replaced my_canvas['img'] with, but it doesn't seem to work.
    2. I would like to have a visible mouse cursor when I pull up the image. I know there is a Mouse.show_cursor(show=True) function, but I don't think i'm assigning it correctly. Entering that exact line of code into my script on its own does not seem to work.

    from openexp.canvas import canvas

    from PIL import Image

    from openexp.mouse import mouse

    my_canvas = canvas(exp)

    my_canvas.image(exp.get_file('gaze_left.png'))

    my_mouse = mouse(exp)

    while True:

     my_canvas.show()

     button, pos, timestamp = my_mouse.get_click()

     # Break loop on left click

     if button == 1:

    break

     # Scroll up

     if button == 4:

    my_canvas.y -= 10

     # Scroll down

     elif button == 5:

    my_canvas.y += 10



    Thank you for any help!


    Best,

    Sam

  • Hi Sam,

    1

    Replace my_canvas.image(exp.get_file('gaze_left.png')) with

    my_canvas['img'] = Image(exp.get_file('gaze_left.png'))

    That should do the trick. If not, rather than doing my_canvas.y+=10 you have to draw the image to the canvas every time you want to update the position new (with adjusted x and y coordinates)

    2

    Have you seen that part of the documentation:

    Note: In most cases, you will want to use the visible keyword, which changes the visibility during response collection, that is, while mouse.get_click() is called. Calling show_cursor() will not implicitly change the value of visible, which can lead to the somewhat unintuitive behavior that the cursor is hidden as soon as get_click() is called.

    So button,pos,timestamp=my_mouse.get_click(visible=True) should do the trick.

    Eduard

    Buy Me A Coffee

  • Thank you so much Eduard! (bought you a coffee). That solved my second issue, the mouse problem. For my first issue, When I replaced my code: my_canvas.image(exp.get_file('gaze_left.png')) with what you suggest: my_canvas['img'] = Image(exp.get_file('gaze_left.png')) I get the following error


    phase: prepare

    item: inline_script

    line: 9

    exception message: 'module' object is not callable

    exception type: TypeError


    I worry that I cannot redraw the image with new x,y coordinates every time I need it to shift, as I need it to shift in response to the mouse scroll response. I'm not sure I know how to redraw the image with new coordinates in response to a mouse scroll response.


    Thank you again!


    Best,

    Sam

  • Hi @eduard ,

    Nevermind! I was able to use your suggestion to draw lower/higher version of the image, combined with a "run if" condition based on a scrolling response. EXTREMELY helpful, thank you and @sebastiaan so much!

  • HI Sam,

    Thanks! Glad it worked out :)

    Eduard

    Buy Me A Coffee

Sign In or Register to comment.