you shouldn’t hardcode the 2.66666
import direct.directbase.DirectStart
from direct.gui.DirectGui import DirectButton
from direct.task import Task
from random import choice, randint
from string import ascii_letters
def changeBtnText(task):
gap=.05 # gap between 2 buttons
x=-base.camLens.getAspectRatio()-gap*.5
for b in buttons:
# randomize text
b['text'] = '< -%s >' %((choice(ascii_letters)+'-')*randint(2,8))
b.resetFrameSize() # force update after changing text
bLen = gap + (b.getBounds()[1]-b.getBounds()[0])*b.getSx()
b.setX(x+bLen*.5)
x+=bLen
task.delayTime=1 # set task delay
return Task.again
btn1=DirectButton(text='xxxxxx',scale=.06,pos=(0,0,.95))
btn2=DirectButton(text='xxxxxx',scale=.04,pos=(0,0,.95))
btn3=DirectButton(text='xxxxxx',scale=.02,pos=(0,0,.95))
buttons=[btn1,btn2,btn3]
# to avoid the first delay, don't use taskMgr.doMethodLater,
# add it normally, then set the delay in the task
taskMgr.add(changeBtnText,'changeBtnText')
run()