Yony
Yony

Reputation: 1284

wx.StaticBox size parameter not respected

I'm trying to create a wx.StaticBox having a constant size, regardless to the size of the widget within. So I have a panel containing the following code:

box = wx.StaticBox(self, -1, 'BoxTitle', size=(200, 200))
bsizer = wx.StaticBoxSizer(box, wx.VERTICAL)

text = wx.StaticText(self, -1, 'Text')
bsizer.Add(text)

border = wx.BoxSizer()
border.Add(bsizer)
self.SetSizer(border)

However, the box simply wraps the StaticText widget within, instead of sticking to the specified 200x200 size. How do I make the box conform to a hard-coded size?

Upvotes: 0

Views: 1021

Answers (1)

Mark Gemmill
Mark Gemmill

Reputation: 5949

If you add the following after creating your StaticBox it will force the size for you:

box.SetMaxSize((200,200))

Note that a StaticText box does not wrap its text, so if your text is wider than the StaticBox it will extend beyond the edge of the box (as least on OS X it does this, may behave differently on Windows).

Update:

On Windows the sizing behaves much differently - the StaticBox just shrinks around the text.

Although I agree with bogdan that explicitly setting a window size with SetMinSize or SetMaxSize is probably not a good idea (e.g. if system font size changes and the contents no longer fits nicely into the size constraints you've set), the StaticText control shrinks to the size of its contents and appears to pull everything with it unless you explicitly force size limits on its container.

I suspect your only option here is either to use SetMaxSize or SetMinSize on your box control, or try other control options other than a StaticText. You could try the HtmlWindow as an alternative.

Upvotes: 1

Related Questions