Updating statictext allignment

This should be easy to do although I can't work it out. All I'm after are two
Static Texts aligned back to back (left one right aligned and the right one left
aligned) and centred on the window, and to stay that way when I update their
labels.

I've included some demo code to show what I mean. Cheers!

import wx

class MyFrame(wx.Frame):
  def __init__(self, parent, id):
    wx.Frame.__init__(self, parent, id, "Test",size= wx.Size(600,200))

    panel = wx.Panel(self, -1)

    sizer= wx.BoxSizer(wx.VERTICAL)
    
    sizer.AddStretchSpacer()
    
    text_sizer= wx.BoxSizer(wx.HORIZONTAL)
    self.text= wx.StaticText(panel,-1,"starteeeeeeeeeeeeeeeeee")
    self.text.SetBackgroundColour("black")
    self.text.SetForegroundColour("white")
    text_sizer.Add(self.text,1,wx.ALIGN_RIGHT|wx.ALL,10)
    self.text2= wx.StaticText(panel,-1,"sswdswd")
    self.text2.SetBackgroundColour("black")
    self.text2.SetForegroundColour("white")
    text_sizer.Add(self.text2,1,wx.ALIGN_LEFT|wx.ALL,10)
    sizer.Add(text_sizer,0,wx.ALIGN_CENTER)
    
    btn_sizer= wx.BoxSizer(wx.HORIZONTAL)
    btn= wx.Button(panel,-1,"Change Label")
    btn_sizer.Add(btn,0,wx.ALIGN_CENTER)
    sizer.Add(btn_sizer,0,wx.ALIGN_CENTER)
        
    sizer.AddStretchSpacer()
    
    panel.SetSizer(sizer)
    
    labels=
["aa","AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","sssssss","#########################
###"]
    self.labels= (i for i in labels)
    self.labels2= (i for i in labels[::-1])
    
    self.Bind(wx.EVT_BUTTON,self.change,btn)
    
    self.Center()
    
  def change(self,event):
    self.text.SetLabel(self.labels.next())
    self.text2.SetLabel(self.labels2.next())
    self.text.GetParent().Layout()

class MyApp(wx.App):
  def OnInit(self):
    frame = MyFrame(None, -1)
    frame.Show(True)
    return True

if __name__ == "__main__":
  app = MyApp(False)
  app.MainLoop()

Since you've set the static text items to have proportion=1 in their sizer then the sizer is not able to do the alignment because the size of the widget will be expanded to fill the space allotted to it. Setting proportion to 0 would not help because then the division between the items will end up being off-center. You can however tell the static text to do the alignment itself, and it will then align the text within the space it is given. See attached. There is still some off-centering done when the total space needed by the items exceeds the space available, but otherwise I think it is doing what you want.

paul2.py (1.61 KB)

···

On 10/5/11 8:20 AM, Paul wrote:

This should be easy to do although I can't work it out. All I'm after are two
Static Texts aligned back to back (left one right aligned and the right one left
aligned) and centred on the window, and to stay that way when I update their
labels.

--
Robin Dunn
Software Craftsman

Ah Perfect, Thanks!
Wish I'd thought of that now :slight_smile: