wx.Window with wx.Sizer Doesn't Work

When you use a wx.Sizer with a wx.Window (not a derived class, wx.Window itself) it is probably a little surprising to find out that it doesn't work at all. No layout happens. It's as if the sizer isn't there at all. There is a good reason for this, and there is also a very simple workaround.

The reason is that since wx.Window is the base of the window hierarchy then adding auto-layout support to wx.Window would mean that every widget would inherit it, even those that are not intended to be containers of other windows. For example it doesn't make much sense to have wx.CheckBox support auto-layout functionality because it will likely never have child widgets that it needs to manage.

The workaround is very simple, just use a wx.Panel instead of a wx.Window. If for some reason you do want to use a wx.Window and also give it a sizer, then simply bind a handler for EVT_SIZE to the window too, and in the event handler call self.Layout().

wx.Window and wx.Sizer (last edited 2011-06-24 21:17:19 by c-98-246-90-205)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.