pythonuser-interfacelayoutwxpythonsizer

elementary wx sizing issues


I've got the following sizing-related code:

import wx

class TableSelectPanel(wx.Panel):
    def __init__(self, parent, *args, **kwargs):
        wx.Panel.__init__(self, parent, *args, **kwargs)

        self.title = wx.StaticText(self, label="Select Table")
        self.tableList = wx.ListBox(self)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.title)
        sizer.Add(self.tableList, flag=wx.EXPAND)
        self.SetSizerAndFit(sizer)

class LobbyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        
        self.tableSelect = TableSelectPanel(self)
        
        #window size
        self.SetMinSize((800, 600))
        self.SetMaxSize((800, 600))
        self.SetSize((800, 600))
        
        #sizers
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.tableSelect, flag=wx.EXPAND)
        
        self.SetSizer(sizer)

        self.Show(True)

What I expect is that I will have an 800x600 window with the wx.ListBox stretching vertically to fit the entire height of the table. However, while I do have an 800x600 window, the wx.ListBox does not expand to the entire height. Rather, it seems the panel does stretch out, but the list box does not:

Incorrect Layout

What have I done wrong?


Solution

  • Set the proportion to 1:

    sizer.Add(self.tableList, proportion=1, flag=wx.EXPAND)
    

    Although the meaning of this parameter is undefined in wxSizer, it is used in wxBoxSizer to indicate if a child of a sizer can change its size in the main orientation of the wxBoxSizer - where 0 stands for not changeable and a value of more than zero is interpreted relative to the value of other children of the same wxBoxSizer. For example, you might have a horizontal wxBoxSizer with three children, two of which are supposed to change their size with the sizer. Then the two stretchable windows would get a value of 1 each to make them grow and shrink equally with the sizer's horizontal dimension.