Sign in with your CogSci, Facebook, Google, or Twitter account.

Or register to create a new account.

We'll use your information only for signing in to this forum.

Supported by

Text width in pixels on Mac OSX

KiriKiri Posts: 23
edited October 2016 in Expyriment

I got a problem with some strings I'd like to be presented in a specific way.
Each string is on a specific canvas, which are presented the right way. But now I'd like to change the x-coordinate of the presented canvasses respectively to the text-width (in pixels) of the canvas presented before.
I could only find solutions for Windows (like this: http://stackoverflow.com/questions/32555015/how-to-get-the-visual-length-of-a-text-string-in-python). Since I am bound to Mac OSX atm, I'd like to ask you if you know about an equivalent for Mac?
I am using MacOS Sierra 10.12 and OpenSesame 3.1.3.


  • fladdfladd Posts: 69

    Dear Kiri,

    you can use the surface_size attribute of the stimulus to get an estimation of the width of your text stimulus, based on the underlying Pygame surface on which the text is presented on. This will work independently from the OS. However, keep in mind that, while this estimate seems not too bad for the width (as opposed to the height), as can be seen in the attached example, it is not guaranteed to be accurate. So, depending on what you need the text width for, if you need more precision (e.g. for calculating visual angles), you will not get around actually measuring the stimulus size on screen.


    import expyriment as xpy
    exp = xpy.control.initialize()
    s = xpy.stimuli.TextLine("WWW", text_colour=xpy.misc.constants.C_GREEN,
    print s.surface_size

    I hope this helps. Let me know if you have any further questions.


    Thanked by 1Kiri

    Florian Krause (Developer)

  • KiriKiri Posts: 23

    Hi Florian,

    thank you very much. I got very near to a solution.
    So far, I defined the following function in my code:

    def GetSize(text): 
        exp = xpy.control.initialize()
        s = xpy.stimuli.TextLine(text)
        return s.surface_size

    Since I want to "write a sentence" by printing a word behind another word (which should not be presented anymore) I use this function within an inline-script in a for-loop:

    for i, word in enumerate(sentence):
            z = GetSize(word)
            x = x + int(z[i])

    I hope you see where I want to go from there. Unfortunately, I get the error message IndexError: tuple index out of range even if the sentence is not finished. Everything else seems to work. Since I am not that used to programming with python, I'd like to ask for another tipp at this stage.

    Thank you very much (also in advance).


  • fladdfladd Posts: 69

    Hi Kiri,

    I am afraid I don't entirely understand what you are trying to do. In general, there is only need to initialize Expyriment once, at the beginning of your script (see also: http://docs.expyriment.org/Tutorial.html). I also don't understand what you mean with "inline". Could you maybe send me your whole Expyriment script? Then I can have a look.


    Florian Krause (Developer)

  • KiriKiri Posts: 23

    Hi fladd,

    sorry, that I needed so much time. I've conducted my experiment now by showing the words in the center of the screen (which is standard for OS canvasses).

    Unfortunately, it's impossible to send the whole script.

    With "inline" I meant the whole script itself with both, prepare-phase and start-phase.

    What I originally tried to do was to alternate the coordinates of the words shown on the canvasses, respectively to the length of the words shown before. Length should not have been measured by letters, but by the width of the whole word in pixels. With Windows, there's a possibility as shown in the first link. But MacOS does not have such a library for fonts and their pixel-width (afaik).

    I now used the standard-method with centered text-presentation. But maybe someone might need a solution for a similar or even the same problem I had.

    So thank you for your support and have a nice Christmas.


Sign In or Register to comment.