Howdy, Stranger!

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

Supported by

[solved] Implementing Forms with Canvas Objects

edited April 2014 in OpenSesame

Hi all,

Apologies if this issue has already arisen. I've had a look and didn't see this anything that answered this question so am starting a new thread. I'm very new to Opensesame so I suspect the answer to this question will be fairly straightforward.

My experiment requires participants to view a series of objects (e.g. rectangles, circles, text) and respond to these stimuli by pressing a button (from a series of 11) on the screen. I'm managed to use the canvas objects to create my stimuli via the inline script facility. And have also managed to use form objects to create the button series. My problem is that I cannot seem to get the canvas objects to be presented on the same screen as the form objects. For example, when I present the canvas objects via the .show command, these objects will disappear as soon as I execute the form via the form._exec() command. How do I get the canvas objects and the form objects to present simultaneously?

Cheers.
Tim

Comments

  • edited 3:00PM

    Hi Tim,

    You cannot draw on forms in the same way that you can with canvas objects, so what you want to do (combining form widgets with custom drawing operations) is difficult. Since you're already familiar with inline_scripts, you could consider the following alternative.

    Step 1: Draw your images using PIL (Python imaging library) and save them as temporary images to the file pool. The following script (to be pasted in the prepare phase of an inline_script item) shows the general idea, but PIL is very well documented and easy to use.

    import os
    from PIL import Image, ImageDraw
    # Create a 100x100 image with PIL and draw a diagonal red line in it. See also:
    # - http://effbot.org/imagingbook/pil-index.htm
    im = Image.new('RGB', (100, 100))
    dr = ImageDraw.Draw(im)
    dr.line((0, 0, 100, 100), fill='red')
    # Save it as `diagonal_line.png` to the file pool
    im.save(os.path.join(exp.pool_folder, 'diagonal_line.png'))
    

    Step 2: Include the just-generated images in a form using the image or image_button widgets. For example (this is OpenSesame script, not Python):

    widget 0 0 1 1 image_button path="diagonal_line.png"
    

    Does that help at all?

    Cheers,
    Sebastiaan

  • edited 3:00PM

    Hi Sebastiaan,

    Sorry for the delayed reply. I was waiting to see if any more issues arose, and nothing has yet! So far so good. Thanks for your help.

    Regards,
    Tim

  • edited 3:00PM

    Ok, it seems I spoke too soon. I'm trying to use the 'ImageFont.truetype' command to specify font type and size. However, when I use this command, I get the following error:

    "ImportError: The _imagingft C module is not installed"

    The script runs fine when I don't use that command. However, I'd like to be able to specify font type and size.

    Any ideas?

    Cheers,
    Tim

  • edited 3:00PM

    Hi Tim,

    Right, that's because the standard distribution of PIL is not compiled with support for TrueType fonts. What you could try is replace the PIL module (which is a subfolder of OpenSesame) by a PIL version that is compiled with TrueType support. That will require a bit of Googling though—I assume it's possible, but I don't know right away how to do it.

    Another option would be to use the pygame drawing routines, which support TrueType out of the box. The script below does the same as the script above, but using PyGame instead of PIL. There's no real advantage to using one over the other (except TrueType support, obviously).

    import pygame
    import os
    # Create a 100x100 image with PyGame and draw a diagonal red line in it.
    # See also:
    # - http://www.pygame.org
    im = pygame.Surface( (100, 100) )
    pygame.draw.line(im, pygame.Color('red'), (0, 0), (100, 100))
    # Save it as `diagonal_line.png` to the file pool
    pygame.image.save(im, os.path.join(exp.pool_folder, 'diagonal_line.png'))
    

    Cheers,
    Sebastiaan

  • edited 3:00PM

    Pygame worked incredibly well. Thanks a lot Sebaastiaan! For anyone else using Pygame, I found this website very useful.

    http://www.pygame.org/docs/ref/surface.html

Sign In or Register to comment.