Howdy, Stranger!

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

Supported by

[solved] Forms

edited January 2013 in OpenSesame

Greetings.
Since the release of 0.27, I've been converting many of the items in my experiment to forms using Python scripting. But how do you use the extra functions, such as "label.draw_text(text, html=True)", etc.?

Best regards,

Jákup

Comments

  • edited 3:43PM

    Hi Jákup,

    These functions are mostly for internal use, so it's debatable whether they should be included in the documentation. But you could, in principle, use them in an inline_script.

    Essentially, most of these functions, such as draw_text() render a specific part of the form to a canvas. For example, let's say that you have a label widget myLabel with the text "My text", which is part of a form called myForm. If you then call

    myForm._exec()

    to execute the complete form, this will (among other things) call

    myForm.render()

    to render the form onto a canvas that is a property of the form (myForm.canvas). This will in turn (among other things) call

    myLabel.render()

    to draw the label, which will (among other things) call

    myLabel.draw_text("My text")

    to draw the actual text. Does that make sense?

    I don't immediately see any situations where calling functions such as draw_text() would be useful. But it is possible to do so, for example to render only some parts of the form. In the snippet below, you can see how a form (from this example) is constructed and then rendered partially by calling specific functions on the widgets (rather than form._exec() or form.render()):

    [pastebin:yXMtG0Be]

    The result will be an incompletely rendered form, like this:

    image

    Hope this clears things up, and happy holidays!

    Cheers,
    Sebastiaan

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • edited 3:43PM

    Dear Sebastiaan.

    Thank you very much for your lucid reply. This makes things much more clear for me. The reason I wanted to know, is the "html=True" parameter in the draw_text() function. If this would allow for using html code to format your text displays using forms, than that would indeed be a very powerful thing. I have also been trying to use the draw_frame() function, so I could draw a frame around different cells of the form. But alas, also unsuccessful.

    How would you do these things in the simple hello world example below?

    from libopensesame import widgets
    form = widgets.form(self.experiment,)
    text = widgets.label(form, center=True, text="Hello  world")
    text.draw_text(text, html=True)
    text.draw_frame(rect=[10, 10, 100, 10], style='normal')
    form.canvas.show()
    

    Best regards,

    Jákup

  • edited December 2012

    Ah, I see. To use HTML you don't need to do anything special: You can type your HTML tags (just a small subset actually) directly into your sketchpad, form, inline_script, etc.

    Edit: Only the Xpyriment back-end currently supports all the tags shown in the documentation

    For more info, see

    Cheers!

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • edited 3:43PM

    Hello again.
    And thank you for your fast reply! :)

    I have played around with some of the canvas HTML codes, but I haven't gotten alignment tags to work. The thing is, I would like to do a custom form, with two text columns, where the one on the left is right justified, and the one on the right is left justified.

    Oh, and happy holidays to you too btw! :)

    Best regards,

    Jákup

  • edited 3:43PM

    Alignment tags are not supported, unfortunately. The only tags that have been implemented are the ones shown in the example. You can pass the center keyword to a label widget, but this just toggles between left and center alignment, without a possibility for right alignment.

    As a crude workaround, though, you could pad you text with spaces to make it seem right aligned. For example, this script will show a left and right aligned column side by side. Note that this will only work properly if you select a monospace font, and if you set center to no.

    __text1__
    This text
     is right
      aligned
    __end__
    
    __text2__
    This text
    is left  
    aligned  
    __end__
    
    widget 0 0 1 1 label text=[text1] center=no
    widget 1 0 1 1 label text=[text2] center=no
    widget 0 1 2 1 button

    Hope this helps!

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • edited 3:43PM

    It certainly did :)

    I will use this work-around, although I will have to change the font after this item. I know this is possible using canvas functions, but is it possible with forms as well? Or, to put it another way: is it possible to map a canvas text to a form cell?

    And thanks for the heads-up about the backend! I've been working with legacy. With xpyriment my formatting tags are working nicely.

    Best regards,

    Jákup

  • edited December 2012

    Hmm... I may have spoken too fast. I cannot get it too work, using canvas functions, to display different fonts on the same canvas. I assume it must be possible, seeing as you can do this with the sketchpad. So what am I missing, for example in the code below?

    canvas.text(text1)
    canvas.set_font(style='mono')
    canvas.text(text2)
    canvas.set_font(style='serif')
    canvas.text(text3)
    canvas.show()
    

    Best regards,

    Jákup

  • edited 3:43PM

    If I understand correctly, your goal is to temporarily use the monospace font (i.e. for the purpose of alignment), but without changing the default font to monospace. Is that correct? If so, the easiest way is probably to define the font style using span tags in a form, like so:

    __text2__
    <span style='mono'>This text
    is left
    aligned</span>
    __end__
    __text1__
    
    <span style='mono'>This text
     is right
      aligned</span>
    __end__
    
    widget 0 0 1 1 label center="no" text="[text1]"
    widget 1 0 1 1 label center="no" text="[text2]"
    widget 0 1 2 1 button

    This way you don't have to change the default font to 'monospace'.

    However, presenting text with different fonts on the same canvas is possible as well. The following script (the full version of your example, essentially) should work:

    [pastebin:gSehHL15]

    In what sense doesn't this work for you? Do you get an error message? (As an aside, for clarity I would avoid using the name canvas for a canvas, because it causes confusion with the class, which is also called canvas.)

    Cheers!

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • edited 3:43PM

    Greetings.

    I just tested your code, and re-tested mine, with all three backends. And the set_font canvas function only seems to work with xpyriment. Is this right?

    Best regards,

    Jákup

  • edited 3:43PM

    Yes, that's a known issue: https://github.com/smathot/OpenSesame/issues/120

    For the Psycho back-end there's currently no real way around this: On some systems the back-end simply ignores the font altogether and falls back to some 'sans serif' default. I think this has to do with the different font naming schemes on different systems, but I have to look into it (it definitely needs to be fixed).

    For the legacy back-end, the problem is that the canvas refuses to forget a previous font, and you can workaround it like this:

    [pastebin:uAYH1iuf]

    Cheers!

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • edited 3:43PM

    Yes that did the trick! Thank you very much for that :)

    Best regards,

    Jákup

Sign In or Register to comment.