Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wxpython flag=wx.EXPAND vs propotion=number

I can't get to understand this: in sizer.Add, proportion is set to identify the scaling ratio of the children widget, and wx.EXPAND tells the children to expand to occupy the available width. but when not setting the flag to wx.EXPAND and setting the proportion to 1, the children widget still expands.

So what is the relation/distinction between these 2 things ?

Thanks


1 Answers

The proportion is specific to the sizer type (a wx.GridSizer will create X*Y cells of equal size so the proportion is not used).

With a wx.BoxSizer, the proportion specifies the scale of an element along the axis of the sizer. Adding elements to a wx.BoxSizer(wx.HORIZONTAL) with a proportion > 0 will scale the items on the horizontal axis. An element with proportion=2 will be scaled to twice the size of an element with proportion=1. A proportion of 0 indicates that the element should not be scaled.

wx.EXPAND specifies that the element should grow to fill the space available along the other axis. Adding elements to a wx.BoxSizer(wx.HORIZONTAL) with wx.EXPAND will expand the items on the vertical axis.

This demo will demonstrate the effect:

import wx

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='BoxSizer demo (resize the frame)')
        self.SetBackgroundColour(wx.WHITE)
        layout = wx.StaticBoxSizer(wx.StaticBox(self,label="wx.BoxSizer(wx.HORIZONTAL)"),wx.HORIZONTAL)
        # proportion=1
        layout.Add( wx.StaticText(self,label='proportion=1', style=wx.BORDER_DOUBLE ), 1, wx.ALL, 10 )
        # proportion=0, expand
        layout.Add( wx.StaticText(self,label='wx.EXPAND', style=wx.BORDER_DOUBLE ), 0, wx.EXPAND|wx.ALL, 10 )
        # proportion=0, align bottom
        layout.Add( wx.StaticText(self,label='wx.ALIGN_BOTTOM', style=wx.BORDER_DOUBLE ), 0, wx.ALIGN_BOTTOM|wx.ALL, 10 )
        # proportion=2, expand
        layout.Add( wx.StaticText(self,label='proportion=2 + wx.EXPAND', style=wx.BORDER_DOUBLE ), 2, wx.EXPAND|wx.ALL, 10 )
        self.SetSizerAndFit(layout)

app = wx.App(False)
Frame().Show()
app.MainLoop()

The next demo demonstrates horizontal box sizers within a vertical box sizer:

import wx

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='BoxSizer demo (resize the frame)')
        self.SetBackgroundColour(wx.WHITE)
        layout = wx.StaticBoxSizer(wx.StaticBox(self,label="wx.VERTICAL"),wx.VERTICAL)
        # row 1
        row = wx.wx.StaticBoxSizer(wx.StaticBox(self,label="wx.HORIZONTAL - proportion=0"),wx.HORIZONTAL)
        row.Add(wx.StaticText(self,label="proportion=0",style=wx.BORDER_DOUBLE),0, wx.ALL,10)
        row.Add(wx.StaticText(self,label="proportion=1",style=wx.BORDER_DOUBLE),1, wx.ALL,10)
        layout.Add(row) # add row 1 without arguments
        # row 2
        row = wx.StaticBoxSizer(wx.StaticBox(self,label="wx.HORIZONTAL - proportion=1"),wx.HORIZONTAL)
        row.Add(wx.StaticText(self,label="proportion=1",style=wx.BORDER_DOUBLE),1, wx.ALL,10)
        row.Add(wx.StaticText(self,label="proportion=2, wx.EXPAND",style=wx.BORDER_DOUBLE),2, wx.EXPAND|wx.ALL,10)
        layout.Add(row,1) # add row 2 with proportion = 1
        # row 3
        row = wx.StaticBoxSizer(wx.StaticBox(self,label="wx.HORIZONTAL - proportion=0, wx.EXPAND"),wx.HORIZONTAL)
        row.Add(wx.StaticText(self,label="proportion=0",style=wx.BORDER_DOUBLE),0, wx.ALL,10)
        row.Add(wx.StaticText(self,label="proportion=1",style=wx.BORDER_DOUBLE),1, wx.ALL,10)
        layout.Add(row,0,wx.EXPAND) # add row 3 with proportion = 0 and wx.EXPAND
        self.SetSizerAndFit(layout)

app = wx.App(False)
Frame().Show()
app.MainLoop()
like image 143
Anonymous Coward Avatar answered Sep 02 '25 14:09

Anonymous Coward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!