How to think about wx.EXPAND?

The following produces a bitmap control that is one third the window size in each direction, at the top left corner, clipping the bitmap - expected behavior.

class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, parent=None, id=wx.ID_ANY,
                          title="Subclass BitmapCTRL Test", size=(WIDTH,HEIGHT))
        
        self.bitmap = wx.Bitmap.FromRGBA(WIDTH//2,HEIGHT//2, 0xFF,0xFF,0xFF ,wx.ALPHA_OPAQUE)
        self.bitmapCTRL = MyBitmap(parent = self,bitmap = self.bitmap,
                                   size=(WIDTH//3,HEIGHT//3))

    def CreateSizers(self):
        self.mainSizer = wx.StaticBoxSizer(wx.HORIZONTAL, self)
        self.SetSizer(self.mainSizer)
        

    def Arrange(self):
        self.mainSizer.Add(self.bitmapCTRL)

The following code, identical to the first except for the indicated “wx.EXPAND” flag, produces a bitmap control that is half the size of the frame horizontally, one third the size of the frame vertically, and centered. The size is expected, as the Sizer has a wx.HORIZONTAL orientation. The centering is unexpected. Why does this happen?

class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, parent=None, id=wx.ID_ANY,
                          title="Subclass BitmapCTRL Test", size=(WIDTH,HEIGHT))
        
        self.bitmap = wx.Bitmap.FromRGBA(WIDTH//2,HEIGHT//2, 0xFF,0xFF,0xFF ,wx.ALPHA_OPAQUE)
        self.bitmapCTRL = MyBitmap(parent = self,bitmap = self.bitmap,
                                   size=(WIDTH//3,HEIGHT//3))

    def CreateSizers(self):
        self.mainSizer = wx.StaticBoxSizer(wx.HORIZONTAL, self)
        self.SetSizer(self.mainSizer)
        

    def Arrange(self):
        self.mainSizer.Add(self.bitmapCTRL, wx.EXPAND)

Hold up, I’ve got it.

self.bitmap = wx.Bitmap.FromRGBA(WIDTH,HEIGHT//2, 0xFF,0xFF,0xFF ,wx.ALPHA_OPAQUE)

Now creates a bitmap that fills the frame horizontally. So:

  • The bitmap is centered within the StaticBitmap control () (always?)
  • The wx.EXPAND flag expands the StaticBitmap control to fill the frame, showing as much of the bitmap as there is to show.

Close.

Sizer item flags tell the sizer how to align or size the item within the space allocated to it by the sizer and which sides to put the margins. The proportion parameter is is also used in some cases. For box sizers the proportion parameter controls how the item is sized in the primary dimension (horizontal or vertical) and the alignment flags and wx.EXPAND flag control how the item is sized in the alternate dimension. The wx.EXPAND will enlarge the item as much as it can within those constraints.

2 Likes

Thanks for this explanation. I had been struggling with how to think about this. Perhaps, that explanation can go into the SizedControls demo and documentation. This helped me fixed some alignment and sizing problems I was having in some of my custom dialogs