Align text with the centre of a textbox (TextBox2)

How can I align a TextBox2 stimulus to be in the center of a textbox? I tried changing the alignment attribute, but it had no effect (for Psychopy v20201.1.3). Here is my example code:

from psychopy import visual, logging
from psychopy.hardware import keyboard

win = visual.Window(
    size=[1536, 864], fullscr=True, screen=0,
    winType='pyglet', allowGUI=False, allowStencil=False,
    monitor='testMonitor', color=[0,0,0], colorSpace='rgb',
    blendMode='avg', useFBO=True,
    units='height')

textbox = visual.TextBox2(
     win, text='Hallo', font='Arial',
     pos=(0, 0), letterHeight=0.05,
     size=(0.9, 0.2), borderWidth=2.0,
     color='white', colorSpace='rgb',
     opacity=1,
     bold=False, italic=False,
     lineSpacing=1.0,
     padding=None,
     anchor='center',
     fillColor=None, borderColor='blue',
     flipHoriz=False, flipVert=False,
     editable=False,
     name='instr1_textbox',
     autoLog=True,
)
key_resp = keyboard.Keyboard()

pos = [(0,0), (0.2, 0.2), (-0.2, 0.2), (-0.2, -0.2), (0.2, -0.2)]
aligns = ['center', 'left']

textbox.autoDraw = True

for  this_pos in pos:
    for this_align in aligns:
        textbox.pos=this_pos
        textbox.alignment=this_align
        textbox.text=str(this_pos) + ' ' + this_align
        
        while not key_resp.getKeys():
            win.flip()

I think I found a solution for some use cases: I just set textbox.size=[None, None].

That doesn’t align the text in the center of the textbox, but it fits the bounding box around the text, so that it has the same effect. However, I would still be interested if someone knows how to align the text in the center of a textbox if the bounding box is set at a fixed size, e.g. (0.9, 0.2) as in my example above.

This is something we’re working on and intending to add! It’s one of the things we need to sort out before taking the “beta” sticker off of TextBox.

1 Like

Thank you for your reply!

Hi Todd,
Does the current functionality allow to have 5 textboxes appear simultaneously in the same row?
I put them in different locations to make sure they appear separately but they overlap…
Please advise,
Aya

How far apart are their positions? If the distance between them is less than their width then I would expect them to overlap