I’m creating a StaticText element with multiple child StaticText elements that aren’t quite sizing correctly. I’m sure it’s a BoxSizer issue, but maybe not? Here’s a screenshot of what’s happening: http://cloud.smallparade.com/CMxC
That should read:
Wilco
One Sunday Morning (Song
For Jane Smiley's Boyfriend)
The Whole Love (Deluxe Edition)
As you can likely deduce, the second and third lines (Album) are one StaticText element that’s wrapping but not expanding to fit its contents.
Here is the code that creates this element:
def PlayLabel(self, artist, track, album):
header_font = wx.Font(26, wx.SWISS, wx.NORMAL, wx.BOLD, False, "Helvetica")
album_font = wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD, False, "Helvetica")
# label container
block = wx.StaticText(self.Sidebar, -1, '')
block.sizer = wx.BoxSizer(wx.VERTICAL)
block.SetBackgroundColour('WHITE')
label_artist = wx.StaticText(block, -1, artist)
label_artist.SetForegroundColour('BLACK')
label_artist.SetFont(header_font)
label_track = wx.StaticText(block, -1, track)
label_track.SetForegroundColour('BLACK')
label_track.SetFont(header_font)
label_album = wx.StaticText(block, -1, album)
label_album.SetForegroundColour('BLACK')
label_album.SetFont(album_font)
block.sizer.AddMany([
(label_artist, 1, wx.EXPAND|wx.TOP|wx.LEFT|wx.RIGHT, 5),
(label_track, 1, wx.EXPAND|wx.LEFT|wx.RIGHT, 5),
(label_album, 1, wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, 5)
])
block.SetSizerAndFit(block.sizer)
self.Sidebar.sizer.Add(block, 0, wx.EXPAND|wx.ALIGN_LEFT|wx.BOTTOM, 5)
self.Sidebar.sizer.Layout()
self.Layout()
return block
Any idea what’s going on?
EDIT:
Complete code: http://pastie.org/2976327
I’ve never seen anyone create a sizer like that. Normally you do this:
Then you add each element. I suspect that since you’re doing it in a weird way, you’re getting weird results. Don’t use the StaticText’s sizer method for this. They are not container objects. Instead, do this:
And then add the other widgets as you did before. And telling the sizer to set itself won’t work right either: “block.SetSizerAndFit(block.sizer)”. You need to call the parent widget’s SetSizer() method, which is probably the Sidebar here:
or maybe you can just add it the way you had it here:
EDIT
Here’s a simple code example:
I personally don’t like Fit() much, but in this case, it works for me.