Hey, I've been trying to get this to work now for a couple of hours
but I'm having no luck.
I want to have a StaticBox which expands to the edges of the panel,
but the height is dynamic and is heigh enough to hold the text
contained inside (which should wrap).
I guess this is a bit tricky as the box calculates its size based of
the contents, but StaticText wont know what height to return in
DoGetBestSize, because it doesn't know the width it's living in
(changing width will change the wrapping points, so changing the
height) - so it's a bit of a catch 22. The box needs to know the
height from the control, and the control needs to know the width from
the box.
One thing I did discover which I found very weird is that if I wrap
the control a bit shorter (or I guess anywhere) it behaves as I want,
except that the wrapping point(s) always break at those points, even
after resizing. The example below should show what I mean - The second
box works as I want when I resize the frame, but the last word is
always on a new line. Weirdly If I set the wrapping point as the same
width of the control then it doesn't wrap at all (which is the first
box in the example)
import wx
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, size=(800, 300))
self.panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
txt = " ".join("words" for _ in xrange(100)) + " LASTWORD"
for x in [0, 10]:
box = wx.StaticBox(self.panel)
box_sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
text = wx.StaticText(self.panel, label=txt)
text.Wrap(text.GetSize().width - x)
box_sizer.Add(text, 0, wx.EXPAND)
sizer.Add(box_sizer, 0, wx.EXPAND | wx.ALL, 10)
self.panel.SetSizer(sizer)
self.Show()
self.panel.Layout()
if __name__ == "__main__":
app = wx.App(False)
Frame()
app.MainLoop()